2014-06-17 32 views
0

我有一個名爲Awal.java活動和代碼:我的活動不能與我的數據庫連接

public class Awal extends Activity implements OnItemClickListener { 


    private Cursor kategori; 
    private MyDatabase db; 
    private List<String> ktg; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

      setContentView(R.layout.activity_main); 


      db = new MyDatabase(this, null); 
      ktg = db.getKategori(); 



      String namaKtg[] = ktg.toArray(new String[ktg.size()]); 

      ListView listView = (ListView) findViewById(R.id.list); 
      listView.setAdapter(new ArrayAdapter<String>(this, R.layout.list, R.id.label, namaKtg)); 
      listView.setOnItemClickListener(this); 

    } 



    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     kategori.close(); 
     db.close(); 
    } 

    public void onItemClick(AdapterView<?> adapter, View v, int pos, long l) { 

     Intent intent = new Intent(Awal.this,Detail.class); 
     intent.putExtra("namaKategori", adapter.getItemAtPosition(pos).toString()); 
     startActivity(intent); 
    } 

} 

也databaseHelper命名MyDatabase.java這裏是代碼:

package com.mroring.belajarperancis; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.ArrayList; 
import java.util.List; 

import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class MyDatabase extends SQLiteOpenHelper { 
    // Variable declaration 
    private static String DB_PATH = "/data/data/com.mroring.belajarperancis/databases/"; 
    public static String DB_NAME = "MY_DATABASE"; 
    private final Context myContext; 
    private String strMypath; 


    public MyDatabase(Context context, String DB_NAME) { 
     super(context, DB_NAME, null, 1); 
     this.myContext = context; 
     try { 
      MyDatabase.DB_NAME = DB_NAME; 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void createDatabase() throws IOException { 
     try { 
      // check if the database exists 
      boolean dbExist = checkDatabase(); 
      if (!dbExist) { 
       // database is not present copy databse 
       this.getReadableDatabase(); 
       try { 
        copyDatabse(DB_NAME); 
       } catch (IOException e) { 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Check if the database already exist to avoid re-copying the file each 
    * time you open the application. 
    * 
    * @return true if it exists, false if it doesn't 
    */ 
    private boolean checkDatabase() { 
     try { 
      File dbFile = new File(DB_PATH + DB_NAME); 
      return dbFile.exists(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return false; 
    } 

    /** 
    * Copies your database from your local assets-folder to the just created 
    * empty database in the system folder, from where it can be accessed and 
    * handled. This is done by transfering bytestream. 
    * */ 
    public void copyDatabse(String DB_NAME) throws IOException { 
     try { 
      // Open your local db as the input stream 
      Input`enter code here`Stream myInput = myContext.getAssets().open(DB_NAME); 
      // Path to the just created empty db 
      String outFileName = DB_PATH + DB_NAME; 
      // Open the empty db as the output stream 
      OutputStream myOutput = new FileOutputStream(outFileName); 
      // transfer bytes from the inputfile to the outputfile 
      byte[] buffer = new byte[1024 * 2]; 
      int length; 
      while ((length = myInput.read(buffer)) > 0) { 
       try { 
        myOutput.write(buffer, 0, length); 
       } catch (Exception e) { 
       } 
      } 
      if (myOutput != null) { 
       myOutput.flush(); 
       myOutput.close(); 
      } 
      if (myInput != null) 
       myInput.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * This function is used to open the database 
    * 
    * @throws SQLException 
    */ 
    public void openDatabase() throws SQLException { 
     SQLiteDatabase checkDB = null; 
     // Open the database 
     try { 
      strMypath = DB_PATH + DB_NAME; 
      checkDB = SQLiteDatabase.openDatabase(strMypath, null, 
        SQLiteDatabase.OPEN_READWRITE); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     if (checkDB != null) { 
      checkDB.close(); 
     } 
    } 


     public List<String> getKategori() { 
      String query = "SELECT _id FROM kategori"; 
      List<String> kategori = new ArrayList<String>(); 

      SQLiteDatabase db = getReadableDatabase(); 
      Cursor cursor = db.rawQuery(query, null); 

      //looping through 
      if (cursor.moveToFirst()) { 
       do { 
        kategori.add(cursor.getString(0)); // add id & nama 
       } 
       while (cursor.moveToNext()); 
      } 

      cursor.close(); 
      db.close(); 
      return kategori; 
     } 

     public List<List<String>> getDetail(String namaKategori) { 
      String query = "select pra, ina, baca from kata,kategori where kata.id_kategori=kategori._id and kategori.nama = '"+namaKategori+"'"; 
      List<List<String>> detail = new ArrayList<List<String>>(); 

      SQLiteDatabase db = getReadableDatabase(); 
      Cursor cursor = db.rawQuery(query, null); 

      //looping 
      if (cursor.moveToFirst()) { 

       do { 
        List<String> item = new ArrayList<String>(); 
        item.add(cursor.getString(0)); //add french word 
        item.add(cursor.getString(1)); //add indonesian word 
        item.add(cursor.getString(2)); //add baca 
        detail.add(item); 
       } 
       while (cursor.moveToNext()); 
      } 
      cursor.close(); 
      db.close(); 
      return detail; 
     } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     try { 
      copyDatabse(DB_NAME); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
    } 

} 

而這種組合總是會出錯,這裏是錯誤:

06-18 01:23:01.694: E/AndroidRuntime(17219): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mroring.belajarperancis/com.mroring.belajarperancis.Awal}: android.database.sqlite.SQLiteException: no such table: kategori: , while compiling: SELECT _id FROM kategori 
06-18 01:23:01.694: E/AndroidRuntime(17219): Caused by: android.database.sqlite.SQLiteException: no such table: kategori: , while compiling: SELECT _id FROM kategori 

我有一個名爲'MY_DATABASE 'in .../BelajarPerancis/assets/databases/MY_DATABASE還有....../BelajarPerancis/assets/MY_DATABASE(我試圖把數據庫放在兩個地方)。

我試圖從DDMS拉數據庫,它返回相同的內容/表的數據庫,它有表'Kategori'。

我的'Kategori'表包含'_id'字段,這裏是證明3 Rows returned from: select nama from kategori; (took 5ms)

回答

0
  1. 您在線路通過數據庫名稱爲空db = new MyDatabase(this, null);
  2. 我看不到代碼來創建「駕駛員學校」表 和查詢,你試圖讓作爲「駕駛員學校」 String query = "SELECT _id FROM kategori";該做的表名數據不存在..

變化db = new MyDatabase(this, null);db = new MyDatabase(this, "SOME_DATABASE_NAME");

,然後編寫代碼在數據庫中創建表。

活動啓動不起作用,因爲db.getKategori();是從onCreate()方法調用的,並且拋出異常。

請到通過http://www.vogella.com/tutorials/AndroidSQLite/article.html

希望這可以幫助你!

+0

謝謝!非常非常! –

+0

我只將'null'更改爲我的數據庫名稱,它的工作原理!非常感謝。願上帝保佑你:D –

0

您確定創建數據庫嗎?

我說因爲您使用絕對路徑來獲取數據庫文件,所以最好的做法是創建一個數據庫作爲Google的標準。

這裏是一個很好的答案,如何做到這一點:

How do I create a database in android?

您可以檢查你的表太的名字,如果你用大寫的第一個字母必須以那種方式都使用使用時間。

att。

相關問題