SQLite不会创建表

时间:2015-06-12 08:48:25

标签: android sqlite

你能告诉我我做错了什么吗?我已经使用sqlite为android开发了一些没有任何错误的应用程序。而我通过查看上一个项目中的代码来做到这一点。我也在其他设备上试过这个应用程序,但我仍然收到错误。

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;
    }

}

更新 我一直在尝试从我的上一个项目实现DatabaseAdapter类,并使用android studio编译,它得到相同的错误,最后我总是使用eclipse。请告诉我原因。我不知道该怎么做。

1 个答案:

答案 0 :(得分:-3)

尝试将数据库名称从xxxxx更改为xxxxx.sqlite(如果它不包含extesion)