Android中的两个表(SQLite)

时间:2017-02-04 14:07:01

标签: java android sqlite android-sqlite

我正在尝试在Android中的一个数据库中创建两个表。但我仍然有一个错误,不知道该怎么做。

这是我的DbHelper类

private static final String DB_NAME="DBbudik";
private static final int DB_VER = 2;
//public static final String IDevent = "IDevent";
//Tabulka pre poznámky
public static final String DB_TABLE="Task";
public static final String DB_COLUMN = "TaskName";
//Tabulka pre udalosti
public static final String DB_TABLE2="DayEvent";
public static final String DB_COLUMN1="DayEventName";
public static final String DB_COLUMN2="DayEventPlace";
public static final String DB_COLUMN3="DayEventTime";
public static final String DB_COLUMN4="DayEventDate";
public static final String DB_COLUMN5="DayEventNote";



public DbHelper(Context context) {
    super(context, DB_NAME, null, DB_VER);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String query = String.format("CREATE TABLE %s (ID INTEGER PRIMARY KEY AUTOINCREMENT,%s TEXT NOT NULL);",DB_TABLE,DB_COLUMN);
    String query2 = String.format("CREATE TABLE %s (ID INTEGER PRIMARY KEY AUTOINCREMENT, %s TEXT NOT NULL, %s TEXT NOT NULL, %s TEXT NOT NULL, %s TEXT NOT NULL, %s TEXT NOT NULL);",DB_TABLE2,DB_COLUMN1,DB_COLUMN2,DB_COLUMN3,DB_COLUMN4,DB_COLUMN5);
    db.execSQL(query);
    db.execSQL(query2);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String query = String.format("DELETE TABLE IF EXISTS %s",DB_TABLE);
    String query2 = String.format("DELETE TABLE IF EXISTS %s",DB_TABLE2);
    db.execSQL(query);
    db.execSQL(query2);
    onCreate(db);
}

此错误显示,并指​​向第50行:

FATAL EXCEPTION: main
    Process: studiodoublek.budik, PID: 16327
    java.lang.RuntimeException: Unable to start activity ComponentInfo{studiodoublek.budik/studiodoublek.budik.Java.kalendar}: android.database.sqlite.SQLiteException: near "TABLE": syntax error (code 1): , while compiling: DELETE TABLE IF EXISTS Task
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
         android.app.ActivityThread.access$900(ActivityThread.java:154)
        aandroid.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:234)
        at android.app.ActivityThread.main(ActivityThread.java:5526)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: android.database.sqlite.SQLiteException: near "TABLE": syntax error (code 1): , while compiling: DELETE TABLE IF EXISTS Task
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504)
        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.executeSql(SQLiteDatabase.java:1674)
        at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
        at studiodoublek.budik.Helper.DbHelper.onUpgrade(DbHelper.java:50)

并显示错误。这是第50行

  

db.execSQL(查询);

2 个答案:

答案 0 :(得分:1)

将您的查询更改为:

db.execSQL("drop table if exists  %s",DB_TABLE);

答案 1 :(得分:-1)

使用此方法在单个数据库中创建两个表。

   public static final String DATABASE_NAME = "sy_call_logs";
public static final int DATABASE_VERSION = 4;


public static final String TABLE_NAME_PHONE_CALL_LOG = "phone_call_log";
public static final String COLUMN_PHONE_CALL_LOG_ID = "id";
public static final String COLUMN_PHONE_CALL_LOG_NUMBER = "number";
public static final String COLUMN_PHONE_CALL_LOG_CALL_TYPE = "callType";
public static final String COLUMN_PHONE_CALL_LOG_START_TIME = "startTime";
public static final String COLUMN_PHONE_CALL_LOG_ACTUAL_START_TIME = "actualStartTime";
public static final String COLUMN_PHONE_CALL_LOG_END_TIME = "endTime";
public static final String COLUMN_PHONE_CALL_LOG_CALL_DURATION = "callDuration";
public static final String COLUMN_PHONE_CALL_LOG_CALL_STATUS = "callStatus";
public static final String COLUMN_PHONE_CALL_LOG_CALL_RECORD = "callRecord";


public static final String TABLE_NAME_PHONE_RECORDABLE = "phone_recordable";
public static final String COLUMN_PHONE_RECORDABLE_ID = "id";
public static final String COLUMN_PHONE_RECORDABLE_NUMBER = "contact_number";


public static final String CREATING_TABLE = " CREATE TABLE " + TABLE_NAME_PHONE_CALL_LOG +
        "(" + COLUMN_PHONE_CALL_LOG_ID + " INTEGER PRIMARY KEY, " +
        COLUMN_PHONE_CALL_LOG_NUMBER + " INTIGER, " + COLUMN_PHONE_CALL_LOG_CALL_TYPE + " TEXT, "
        + COLUMN_PHONE_CALL_LOG_START_TIME + " TIMESTAMP, " +
        COLUMN_PHONE_CALL_LOG_ACTUAL_START_TIME + " TIMESTAMP, " + COLUMN_PHONE_CALL_LOG_END_TIME + " TIMESTAMP, " +
        COLUMN_PHONE_CALL_LOG_CALL_DURATION + " TIMESTAMP, " + COLUMN_PHONE_CALL_LOG_CALL_STATUS + " TEXT, " +
        COLUMN_PHONE_CALL_LOG_CALL_RECORD + " TEXT " + ") ";

public static final String CREATE_SELECTED_CONTACT = " CREATE TABLE " + TABLE_NAME_PHONE_RECORDABLE +
        "(" + COLUMN_PHONE_RECORDABLE_ID + " INTEGER PRIMARY KEY, "
        + COLUMN_PHONE_RECORDABLE_NUMBER + " TEXT" + ")";


SQLiteDatabase sqLiteDatabase;


public DbHelper(Context context) {

    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {


    sqLiteDatabase.execSQL(CREATING_TABLE);

    sqLiteDatabase.execSQL(CREATE_SELECTED_CONTACT);

    Log.e("Database OPERATIONS", "Table created...");
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {

    sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME_PHONE_CALL_LOG);
    sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME_PHONE_RECORDABLE);
    onCreate(sqLiteDatabase);

}