使用2个表时,SQLite没有这样的表错误

时间:2016-03-04 10:14:50

标签: android sqlite

我正在做一个Android项目,我使用一个包含多个表的数据库。我第一次尝试使用一张桌子,一切正常,但是当我使用第二张桌子时,我有一个错误 这是代码:
这部分完美无缺:

public class MarqueDBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "reprise.db";
public static final int DATABASE_VERSION = 3;

private static final String SQL_CREATE_ENTRIES = "create table marque (name text primary key)";
private static final String SQL_DELETE_ENTRIES = "drop table if exists marque";

public MarqueDBHelper(Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

public void onCreate(SQLiteDatabase db){
    db.execSQL(SQL_CREATE_ENTRIES);
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
    db.execSQL(SQL_DELETE_ENTRIES);
    onCreate(db);
}
}

这个没有:

public class ModeleDBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "reprise.db";
public static final int DATABASE_VERSION = 3;

private static final String SQL_CREATE_ENTRIES = "create table modele (name text primary key)";
private static final String SQL_DELETE_ENTRIES = "drop table if exists modele";

public ModeleDBHelper(Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

public void onCreate(SQLiteDatabase db){
    db.execSQL(SQL_CREATE_ENTRIES);
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
    db.execSQL(SQL_DELETE_ENTRIES);
    onCreate(db);
}
}

我在此处执行插入时发生错误:

public class ModeleDAO {
private Context context;
private ModeleDBHelper modeleHelper;
private SQLiteDatabase db;

public ModeleDAO(Context context){
    this.context=context;
}

public void addMarque(ArrayList<Modele> marqueListe){
    modeleHelper = new ModeleDBHelper(context);
    db = modeleHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    for(Modele str : marqueListe){
        values.put("name", str.getNom());
        db.insertWithOnConflict("modele", null, values, SQLiteDatabase.CONFLICT_IGNORE);

    }

    db.close();
    modeleHelper.close();
}
}

这是错误:

android.database.sqlite.SQLiteException: no such table: modele (code 1): , while compiling: INSERT OR IGNORE  INTO modele(name) VALUES (?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
at quentin.android.fastback2.DataAccess.ModeleDAO.addMarque(ModeleDAO.java:31)
at quentin.android.fastback2.DataAccess.SyncAdapter$3.onResponse(SyncAdapter.java:143)
at quentin.android.fastback2.DataAccess.SyncAdapter$3.onResponse(SyncAdapter.java:125)
at quentin.android.fastback2.Model.CustomRequest.deliverResponse(CustomRequest.java:42)
at quentin.android.fastback2.Model.CustomRequest.deliverResponse(CustomRequest.java:16)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我尝试了几件事,比如更改数据库版本,卸载/重新安装应用程序以及使用其他设备模拟器(我正在使用Genymotion)
我检查了数据库文件,表“marque”创建得很好但不是表“modele”。就像从未调用过ModeleDBHelper的OnCreate()方法一样。谢谢你的帮助

2 个答案:

答案 0 :(得分:3)

SQLiteOpenHelper版本数据库文件,而不是表格。如果同一数据库文件中有多个表,请将它们全部放在同一个SQLiteOpenHelper

合并帮助程序后,卸载您的应用程序,以便删除旧的数据库文件,并调用创建所有表的onCreate()

答案 1 :(得分:0)

由于您正在创建具有相同名称"reprise.db"的数据库,因此永远不会为您的第二个帮助程序调用on create方法(因为数据库文件已存在)。所以永远不会创建表

public void onCreate(SQLiteDatabase db){ // Never called
   db.execSQL(SQL_CREATE_ENTRIES);
}

根据建议,您应该将它们合并以克服此问题。

所以你的oncreate方法会创建两个表。