我的代码包含一个数据库类。运行它时显示有关CREATE TABLE IF NOT EXISTS语句的错误。请给我解决方法以纠正错误。
clasdbOpenHelper.java
package example.events1;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class classdbOpenHelper extends SQLiteOpenHelper {
public static final String KEY_ROWID = "_id";
public static final String KEY_DESC = "countdesc";
public static final String KEY_DATE = "countdate";
public static final String KEY_EVENT = "countevent";
public static final String DATABASE_NAME= "countdb";
public static final String DATABASE_TABLE = "countable";
public static final int DATABASE_VERSION = 1;
public classdbOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS" + DATABASE_TABLE + "("
+ KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DESC + " TEXT, " + KEY_DATE + " TEXT, " + KEY_EVENT + " TEXT )";
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXITS " + DATABASE_TABLE);
onCreate(db);
}
public Cursor fetchAllEvents() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor mCursor = db.query(DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_DESC, KEY_DATE, KEY_EVENT },
null, null, null, null, null );
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor fetchEventByName(String inputText,String datevalue ) throws SQLException {
SQLiteDatabase db = this.getReadableDatabase();
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = db.query(DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_DESC, KEY_DATE, KEY_EVENT },
null, null, null, null, null);
}
else {
mCursor = db.rawQuery("SELECT * FROM countable WHERE countdesc = ? AND countdate = ?", new String[] {inputText,datevalue});
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public void delete(long id) {
SQLiteDatabase db = this.getReadableDatabase();
db.delete(DATABASE_TABLE, KEY_ROWID + " = ?",
new String[] { String.valueOf(id )});
db.close();
}
}
logcat的
08-20 04:32:26.411: E/SQLiteLog(839): (1) near "EXISTScountable": syntax error
08-20 04:32:26.681: E/AndroidRuntime(839): FATAL EXCEPTION: main
08-20 04:32:26.681: E/AndroidRuntime(839): java.lang.RuntimeException: Unable to start activity ComponentInfo{example.events1/example.events1.Getclicker}: android.database.sqlite.SQLiteException: near "EXISTScountable": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTScountable(_id INTEGER PRIMARY KEY AUTOINCREMENT, countdesc TEXT, countdate TEXT, countevent TEXT )
08-20 04:32:26.681: E/AndroidRuntime(839): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-20 04:32:26.681: E/AndroidRuntime(839): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-20 04:32:26.681: E/AndroidRuntime(839): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-20 04:32:26.681: E/AndroidRuntime(839): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-20 04:32:26.681: E/AndroidRuntime(839): at android.os.Handler.dispatchMessage(Handler.java:99)
08-20 04:32:26.681: E/AndroidRuntime(839): at android.os.Looper.loop(Looper.java:137)
08-20 04:32:26.681: E/AndroidRuntime(839): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-20 04:32:26.681: E/AndroidRuntime(839): at java.lang.reflect.Method.invokeNative(Native Method)
08-20 04:32:26.681: E/AndroidRuntime(839): at java.lang.reflect.Method.invoke(Method.java:511)
08-20 04:32:26.681: E/AndroidRuntime(839): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-20 04:32:26.681: E/AndroidRuntime(839): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-20 04:32:26.681: E/AndroidRuntime(839): at dalvik.system.NativeStart.main(Native Method)
08-20 04:32:26.681: E/AndroidRuntime(839): Caused by: android.database.sqlite.SQLiteException: near "EXISTScountable": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTScountable(_id INTEGER PRIMARY KEY AUTOINCREMENT, countdesc TEXT, countdate TEXT, countevent TEXT )
08-20 04:32:26.681: E/AndroidRuntime(839): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-20 04:32:26.681: E/AndroidRuntime(839): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
08-20 04:32:26.681: E/AndroidRuntime(839): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
08-20 04:32:26.681: E/AndroidRuntime(839): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
08-20 04:32:26.681: E/AndroidRuntime(839): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
08-20 04:32:26.681: E/AndroidRuntime(839): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
08-20 04:32:26.681: E/AndroidRuntime(839): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
08-20 04:32:26.681: E/AndroidRuntime(839): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
08-20 04:32:26.681: E/AndroidRuntime(839): at example.events1.classdbOpenHelper.onCreate(classdbOpenHelper.java:31)
08-20 04:32:26.681: E/AndroidRuntime(839): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
08-20 04:32:26.681: E/AndroidRuntime(839): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
08-20 04:32:26.681: E/AndroidRuntime(839): at example.events1.classdbOpenHelper.fetchEventByName(classdbOpenHelper.java:57)
08-20 04:32:26.681: E/AndroidRuntime(839): at example.events1.Getclicker.onCreate(Getclicker.java:36)
08-20 04:32:26.681: E/AndroidRuntime(839): at android.app.Activity.performCreate(Activity.java:5104)
08-20 04:32:26.681: E/AndroidRuntime(839): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-20 04:32:26.681: E/AndroidRuntime(839): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-20 04:32:26.681: E/AndroidRuntime(839): ... 11 more
答案 0 :(得分:10)
在EXISTS之后给出空格
String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE + "("
+ KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DESC + " TEXT, " + KEY_DATE + " TEXT, " + KEY_EVENT + " TEXT )";
答案 1 :(得分:4)
在您的案例中EXISTS
和表名countable
之间没有空格。