2015-06-12 169 views
0

你能告訴我我做錯了什麼嗎?我已經爲android使用sqlite開發了一些應用程序,沒有任何錯誤。而我通過查看前一個項目中的代碼來實現這一點。我也在另一臺設備上試過這個應用程序,但仍然出現錯誤。SQLite does not create table

Screeenshot using SQLite Browser

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

/** 
* Created by Asus on 6/12/2015. 
*/ 
public class DatabaseAdapter { 

    private static final String DATABASE_NAME = "xxxxxxxxx"; 
    private static final int DATABASE_VERSION = 1; 

    private static final String TABLE_FOLlOW = "follow"; 
    public static final String KEY_FOLLOW_ID = "_id"; 
    public static final String KEY_FOLLOW_KODE = "kode_kategori"; 

    private static final String TABLE_LOKASI = "lokasi"; 
    public static final String KEY_LOKASI_ID = "_id"; 
    public static final String KEY_LOKASI_KODE = "kode_lokasi"; 

    private static final String TABLE_USER = "user"; 
    public static final String KEY_USER_ID = "_id"; 
    public static final String KEY_USER_EMAIL = "email"; 
    public static final String KEY_USER_PASSWORD = "pass"; 

    private static final String CREATE_TABLE_FOLLOW = "CREATE TABLE "+ TABLE_FOLlOW +"("+ KEY_FOLLOW_ID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+ KEY_FOLLOW_KODE +" TEXT NOT NULL)"; 
    private static final String CREATE_TABLE_LOKASI = "CREATE TABLE "+ TABLE_LOKASI +"("+ KEY_LOKASI_ID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+ KEY_LOKASI_KODE +" TEXT NOT NULL)"; 
    private static final String CREATE_TABLE_USER = "CREATE TABLE "+ TABLE_USER +"("+ KEY_USER_ID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+ KEY_USER_EMAIL +" TEXT NOT NULL, "+ KEY_USER_PASSWORD +" TEXT NOT NULL)"; 

    private static final String[] KOLOM_FOLLOW = new String[]{KEY_FOLLOW_ID, KEY_FOLLOW_KODE}; 
    private static final String[] KOLOM_LOKASI = new String[]{KEY_LOKASI_ID, KEY_LOKASI_KODE}; 
    private static final String[] KOLOM_USER = new String[]{KEY_USER_ID, KEY_USER_EMAIL, KEY_USER_PASSWORD}; 

    private Context mCtx; 
    private DatabaseHelper dbHelper; 
    private SQLiteDatabase mDb; 

    private class DatabaseHelper extends SQLiteOpenHelper{ 

     public DatabaseHelper(Context c) { 
      super(c, DATABASE_NAME, null, DATABASE_VERSION); 
      Log.d("DatabaseAdapter", "public DatabaseAdapter(Context c) is running"); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      db.execSQL(CREATE_TABLE_FOLLOW); 
      Log.d("DatabaseAdapter", "Membuat tabel " + TABLE_FOLlOW); 
      db.execSQL(CREATE_TABLE_LOKASI); 
      Log.d("DatabaseAdapter", "Membuat tabel " + TABLE_LOKASI); 
      db.execSQL(CREATE_TABLE_USER); 
      Log.d("DatabaseAdapter", "Membuat tabel " + TABLE_USER); 
      Log.d("DatabaseAdapter","Oncreate method"); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      Log.d("DatabaseAdapter","Upgrade database from "+ oldVersion +" version to "+ newVersion +" version"); 
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_FOLlOW); 
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOKASI); 
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER); 
      onCreate(db); 
     } 
    } 

    public DatabaseAdapter(Context context){ 
     this.mCtx = context; 
     dbHelper = new DatabaseHelper(mCtx); 
     mDb = dbHelper.getWritableDatabase(); 
    } 

    public long storeUserTemp(String email, String password){ 
     ContentValues cv = new ContentValues(); 
     cv.put(KEY_USER_EMAIL, email); 
     cv.put(KEY_USER_PASSWORD, password); 
     return mDb.insert(TABLE_USER, null, cv); 
    } 

    public long storeFollow(String kode){ 
     ContentValues cv = new ContentValues(); 
     cv.put(KEY_FOLLOW_KODE, kode); 
     return mDb.insert(TABLE_FOLlOW, null, cv); 
    } 

    public long deleteAllFollow(){ 
     return mDb.delete(TABLE_FOLlOW, null, null); 
    } 

    public boolean isUserAvailable(){ 
     Cursor cursor = mDb.query(TABLE_USER, KOLOM_USER, null, null, null, null, null); 
     if(cursor.getCount() > 0) 
      return true; 
     else 
      return false; 
    } 

    public boolean isFollow(String kode){ 
     Cursor cursor = mDb.query(TABLE_FOLlOW, new String[]{KEY_FOLLOW_KODE}, KEY_FOLLOW_KODE +"='"+ kode +"'", null, null, null, null); 
     if(cursor.getCount() > 0) 
      return true; 
     else 
      return false; 
    } 

    public String getUserTempEmail(){ 
     Cursor cursor = mDb.query(TABLE_USER, new String[]{KEY_USER_EMAIL}, null, null, null, null, null); 
     String email = cursor.getString(cursor.getColumnIndex(KEY_USER_EMAIL)); 
     return email; 
    } 

    public String getUserTempPassword(){ 
     Cursor cursor = mDb.query(TABLE_USER, new String[]{KEY_USER_PASSWORD}, null, null, null, null, null); 
     String pass = cursor.getString(cursor.getColumnIndex(KEY_USER_PASSWORD)); 
     return pass; 
    } 

    public String[] getAllFollow(){ 
     Cursor cursor = mDb.query(TABLE_FOLlOW, null, null, null, null, null, null); 
     int totalFollow = cursor.getCount(); 
     String[] follow = new String[totalFollow]; 
     if(totalFollow > 0){ 
      for(int i=0; i<totalFollow; i++) 
       follow[i] = cursor.getString(i+1); 
     } 
     return follow; 
    } 

} 

UPDATE 我已經從我的最後一個項目試圖器具DatabaseAdapter類和使用過Android Studio編譯並得到同樣的錯誤,最後我總是使用eclipse。請給我理由。我不知道我該怎麼做。

+0

你叫getWritableDatabase()從任何地方嗎?檢查你的logcat如果onCreate方法已被調用或不 –

回答

-3

嘗試從XXXXX更改數據庫名稱xxxxx.sqlite(如果它不包含extesion)

+0

而且你還需要root權限從應用程序外部訪問數據庫文件。 – Vaizadoo

+0

我的手機已經紮根 –