在SQLite数据库中创建两个或多个表

时间:2018-12-03 09:38:30

标签: android android-sqlite

我正在尝试在SQLite数据库中创建多个表,但是遇到一个我不理解的错误。

这是清楚地说明我在屏幕上看到的图像的图像

image

这是我的代码,我不知道它怎么了。请帮助

class DatabaseHelper extends SQLiteOpenHelper {


public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, version);
}
  public void onCreate(SQLiteDatabase database) {
    createTable(database);// Crear la tabla "gasto"
}
private void createTable(SQLiteDatabase database) {
    String cmd = "CREATE TABLE " + EventProvider.YAFE + " (" +
            EventProvider.Column._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            EventProvider.Column.TITRE + " TEXT, " +
            EventProvider.Column.DATE + " TEXT, " +
            EventProvider.Column.IMAGE + " TEXT," +
            EventProvider.Column.ID_R + " TEXT UNIQUE," +
            EventProvider.Column.STATUT + " INTEGER NOT NULL DEFAULT "+ EventProvider.STATUT_OK+"," +
            EventProvider.Column.INSERTION_EN_ATTENTE + " INTEGER NOT NULL DEFAULT 0)";
    database.execSQL(cmd);

    String table_stand = "CREATE_TABLE_STAND" + EventProvider.STAND + "(" +
            EventProvider.ColumnStand._ID + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
            EventProvider.ColumnStand.TITRE_STAND + "TEXT," +
            EventProvider.ColumnStand.IMAGE_STAND + "TEXT," +
            EventProvider.ColumnStand.ID_R_STAND + "TEXT, UNIQUE" +
            EventProvider.ColumnStand.STATUT_STAND + "INTEGER NOT NULL DEFAULT" + EventProvider.STATUT_OK + "," +
            EventProvider.ColumnStand.INSERTION_EN_ATTENTE_STAND + "INTEGER NOT NULL DEFAULT 0)";
    database.execSQL(table_stand);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    try {
        db.execSQL("drop table " + EventProvider.YAFE);
        db.execSQL("drop table_stand" + EventProvider.STAND);
    }
    catch (SQLiteException e) {
    }
    onCreate(db);
}

3 个答案:

答案 0 :(得分:2)

代替此“ CREATE_TABLE_STAND”

尝试此“创建表支架”

答案 1 :(得分:1)

createTable()的更改

       String table_stand = "CREATE TABLE " + EventProvider.STAND + "(" +
            EventProvider.ColumnStand._ID + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
            EventProvider.ColumnStand.TITRE_STAND + "TEXT," +
            EventProvider.ColumnStand.IMAGE_STAND + "TEXT," +
            EventProvider.ColumnStand.ID_R_STAND + "TEXT, UNIQUE" +
            EventProvider.ColumnStand.STATUT_STAND + "INTEGER NOT NULL DEFAULT" + EventProvider.STATUT_OK + "," +
            EventProvider.ColumnStand.INSERTION_EN_ATTENTE_STAND + "INTEGER NOT NULL DEFAULT 0)";

onUpgrade()的更改

 @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        try {
            db.execSQL("DROP TABLE IF EXISTS " + EventProvider.YAFE);
            db.execSQL("DROP TABLE IF EXISTS " + EventProvider.STAND);
        }

答案 2 :(得分:0)

Here change "CREATE_TABLE_STAND" to CREATE TABLE TableName



String table_stand = "CREATE_TABLE_STAND" + EventProvider.STAND + "(" +
        EventProvider.ColumnStand._ID + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
   *emphasized text*     EventProvider.ColumnStand.TITRE_STAND + "TEXT," +
        EventProvider.ColumnStand.IMAGE_STAND + "TEXT," +
        EventProvider.ColumnStand.ID_R_STAND + "TEXT, UNIQUE" +
        EventProvider.ColumnStand.STATUT_STAND + "INTEGER NOT NULL DEFAULT" + EventProvider.STATUT_OK + "," +
        EventProvider.ColumnStand.INSERTION_EN_ATTENTE_STAND + "INTEGER NOT NULL DEFAULT 0)";
database.execSQL(table_stand);