无法在sqlite android中创建第二个表

时间:2012-06-13 19:19:34

标签: android sqlite

我是使用sqlite数据库android的新手。现在我的android中的sqlite数据库有问题, 首先我创建第一个表:

        private static final String CREATE_TABLE_MASTER = "create table"
        + TABLE_MASTER + " (" + MASTER_ID
        + " integer primary key autoincrement, " + NAMA_MASALAH
        + " Integer not null, " + JLH_KRITERIA + " Integer not null, "
        + JLH_ALTERNATIVE + " text not null);";

它取得了成功,但是当我创建第二个表时:

    private static final String CREATE_TABLE_KRITERIA = "create table if not exist "
    +TABLE_KRITERIA+ "("+ KRITERIA_ID
    + "integer primary key autoincrement, " 
    +KRITERIA_NAMA+ "text);";

但这是错误的,在LogCat中它说:

  

没有这样的表:tableKriteria:在编译时:select tableKriteria,namaKriteria from tableKriteria

这里是我的SQLiteAdapter.java的完整代码:

package com.MFrameworkAHP;

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

public class SQLiteAdapter {
public static final String NAMA_DATABASE = "MY_DATABASE";
public static final String TABLE_MASTER = "MY_TABLE";
public static final int MYDATABASE_VERSION = 1;
public static final String MASTER_ID = "_id";
public static final String NAMA_MASALAH = "Content1";
public static final String JLH_KRITERIA = "Content2";
public static final String JLH_ALTERNATIVE = "Content3";

public static final String TABLE_KRITERIA = "tableKriteria";
public static final String KRITERIA_ID= "idKriteria";
public static final String KRITERIA_NAMA = "namaKriteria";
public static final String KRITERIA_IDDATA = "idDataKriteria";


// create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String CREATE_TABLE_MASTER = "create table"
        + TABLE_MASTER + " (" + MASTER_ID
        + " integer primary key autoincrement, " + NAMA_MASALAH
        + " Integer not null, " + JLH_KRITERIA + " Integer not null, "
        + JLH_ALTERNATIVE + " text not null);";

private static final String CREATE_TABLE_KRITERIA = "create table if not exist "
    +TABLE_KRITERIA+ "("+ KRITERIA_ID
    + "integer primary key autoincrement, " 
    +KRITERIA_NAMA+ "text);";

;
//private static final String

private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;

public SQLiteAdapter(Context c) {
    context = c;
}

public SQLiteAdapter openToRead() throws android.database.SQLException {
    sqLiteHelper = new SQLiteHelper(context, NAMA_DATABASE, null,
            MYDATABASE_VERSION);
    sqLiteDatabase = sqLiteHelper.getReadableDatabase();
    return this;
}

public SQLiteAdapter openToWrite() throws android.database.SQLException {
    sqLiteHelper = new SQLiteHelper(context, NAMA_DATABASE, null,
            MYDATABASE_VERSION);
    sqLiteDatabase = sqLiteHelper.getWritableDatabase();
    return this;
}

public void close() {
    sqLiteHelper.close();
}

public long insert(String namaMasalah, String jlhKriteria, String jlhAlternative) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(NAMA_MASALAH, namaMasalah);
    contentValues.put(JLH_KRITERIA, jlhKriteria);
    contentValues.put(JLH_ALTERNATIVE, jlhAlternative);
    return sqLiteDatabase.insert(TABLE_MASTER, null, contentValues);
}

public long insertKriteria(String namaKriteria){
    ContentValues cv = new ContentValues();
    //cv.put(KRITERIA_ID, idKriteria);
    cv.put(KRITERIA_NAMA,namaKriteria);

    return sqLiteDatabase.insert(TABLE_KRITERIA, null, cv);
}
public int deleteAll() {
    return  sqLiteDatabase.delete(TABLE_MASTER, null, null);

}

public void delete_byID(int id) {
    sqLiteDatabase.delete(TABLE_MASTER, MASTER_ID + "=" + id, null);
}

public void update_byID(int id, String v1, String v2, String v3) {
    ContentValues values = new ContentValues();
    values.put(NAMA_MASALAH, v1);
    values.put(JLH_KRITERIA, v2);
    values.put(JLH_ALTERNATIVE, v3);
    sqLiteDatabase
            .update(TABLE_MASTER, values, MASTER_ID + "=" + id, null);
}

public Cursor queueAll() {
    String[] columns = new String[] { MASTER_ID, NAMA_MASALAH, JLH_KRITERIA,
            JLH_ALTERNATIVE };
    Cursor cursor = sqLiteDatabase.query(TABLE_MASTER, columns, null,
            null, null, null, null);
    return cursor;
}

public Cursor queueKriteria() {
    String[] columns = new String[] { KRITERIA_ID, KRITERIA_NAMA };
    Cursor cursor = sqLiteDatabase.query(TABLE_KRITERIA, columns, null,
            null, null, null, null);
    return cursor;
}

public Cursor queueAll_SortBy_namaMasalah() {
    String[] columns = new String[] { MASTER_ID, NAMA_MASALAH, JLH_KRITERIA,
            JLH_ALTERNATIVE };
    Cursor cursor = sqLiteDatabase.query(TABLE_MASTER, columns, null,
            null, null, null, NAMA_MASALAH);

    return cursor;
}

public class SQLiteHelper extends SQLiteOpenHelper {
    public SQLiteHelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(CREATE_TABLE_KRITERIA);
        db.execSQL(CREATE_TABLE_MASTER);
    }

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

}

}

我已经尝试了大约一个星期来解决我的问题,比如更改了数据库版本,更改了第二个表的create语句等等但是它没有用:( 在这个论坛中有很多问题与我的问题相同,但它无法解决我的问题,例如herehere等。

伙计们,请帮助我。 。

之前谢谢。

3 个答案:

答案 0 :(得分:0)

似乎您可能需要在'create'字符串中添加一些空格。它也应该是exists而不是exist。尝试...

private static final String CREATE_TABLE_KRITERIA = "create table if not exists "
+TABLE_KRITERIA+ " ("+ KRITERIA_ID
+ " integer primary key autoincrement, " 
+KRITERIA_NAMA+ " text);";

答案 1 :(得分:0)

如果您的代码仍然相同,那么您的第一个create table会丢失一个空格。

你有

private static final String CREATE_TABLE_MASTER = "create table"

它应该是

private static final String CREATE_TABLE_MASTER = "create table "

另外,您可以发布错误消息吗?

在onUpgrade()中,您应该执行升级。最快的是删除表然后重新创建。

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.i("MFrameworkAHP", "Upgrading database to " + newVersion);
    db.execSQL("drop table if exists " + TABLE_KRITERIA);
    db.execSQL("drop table if exists " + TABLE_MASTER);
    onCreate(db);
}

   public void onCreate(SQLiteDatabase db) {
        Log.i("MFrameworkAHP", CREATE_TABLE_KRITERIA);
        db.execSQL(CREATE_TABLE_KRITERIA);

        Log.i("MFrameworkAHP", CREATE_TABLE_MASTER);
        db.execSQL(CREATE_TABLE_MASTER);
    }

这将删除升级中不理想的所有内容,但至少会在您更改数据库版本时重新创建测试中的表。

答案 2 :(得分:0)

我刚才遇到了这个问题,解决方法是从开始创建所有表。如果您创建一个只有一个表进行测试的数据库,那么稍后您尝试创建其余的表将无法正常工作。因此,定义一个新的数据库名称并创建您一次需要的所有表格,这应该足够了。