gpt4 book ai didi

Android SQLiteDatabase 语法错误,代码 1

转载 作者:行者123 更新时间:2023-11-29 14:52:16 25 4
gpt4 key购买 nike

我正在制作一个基于 this source 的 SQLiteDatabase , 但我遇到了一个我无法弄清楚的语法错误。我在 StackOverflow 上看过其他关于此的帖子,但它们似乎没有帮助。在那些情况下,它们在某处包含额外的空格或保留字,但我尝试了不同的间距组合并确保没有任何字词是保留字。

我唯一能想到的是我在一个数据库中创建两个表,这是 Not Acceptable 吗?这是导致问题的代码的要点:

//table name
private static final String TABLE_INGREDIENTS = "table_ingredients";
private static final String TABLE_UNITS = "table_units";

//column names
private static final String ING_KEY_ID = "ing_id";
private static final String ING_KEY_NAME = "ing_name";
private static final String ING_KEY_DENSITY = "ing_density";
private static final String UNIT_KEY_ID = "unit_id";
private static final String UNIT_KEY_NAME = "unit_name";
private static final String UNIT_KEY_BASE_VALUE = "unit_base_value";
private static final String UNIT_KEY_FINAL_VALUE = "unit_final_value";
private static final String UNIT_KEY_IS_WEIGHT = "unit_is_weight";

// Create Tables
@Override
public void onCreate(SQLiteDatabase db)
{
String CREATE_ING_TABLE = "CREATE INGREDIENTS TABLE IF NOT EXISTS " + TABLE_INGREDIENTS + "("
+ ING_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + ING_KEY_NAME + " TEXT,"
+ ING_KEY_DENSITY + " REAL" + ")";
db.execSQL(CREATE_ING_TABLE);

String CREATE_UNIT_TABLE = "CREATE UNITS TABLE IF NOT EXISTS " + TABLE_UNITS + "("
+ UNIT_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + UNIT_KEY_NAME + " TEXT,"
+ UNIT_KEY_BASE_VALUE + " REAL," + UNIT_KEY_FINAL_VALUE + " REAL,"
+ UNIT_KEY_IS_WEIGHT + " INTEGER" + ")";
db.execSQL(CREATE_UNIT_TABLE);
}

这是我的日志输出,以防我的解释有误:

02-01 12:48:43.855: D/QBCPro:::(7186): LoadAllTask doInBackground called...
02-01 12:48:43.855: D/QBCPro:::(7186): Entering onResume()...
02-01 12:48:43.875: E/SQLiteLog(7186): (1) near "INGREDIENTS": syntax error
02-01 12:48:43.875: W/dalvikvm(7186): threadid=11: thread exiting with uncaught exception (group=0x41eb8300)
02-01 12:48:43.895: E/AndroidRuntime(7186): FATAL EXCEPTION: AsyncTask #1
02-01 12:48:43.895: E/AndroidRuntime(7186): java.lang.RuntimeException: An error occured while executing doInBackground()
02-01 12:48:43.895: E/AndroidRuntime(7186): at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-01 12:48:43.895: E/AndroidRuntime(7186): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-01 12:48:43.895: E/AndroidRuntime(7186): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-01 12:48:43.895: E/AndroidRuntime(7186): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-01 12:48:43.895: E/AndroidRuntime(7186): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-01 12:48:43.895: E/AndroidRuntime(7186): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-01 12:48:43.895: E/AndroidRuntime(7186): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-01 12:48:43.895: E/AndroidRuntime(7186): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-01 12:48:43.895: E/AndroidRuntime(7186): at java.lang.Thread.run(Thread.java:856)
02-01 12:48:43.895: E/AndroidRuntime(7186): Caused by: android.database.sqlite.SQLiteException: near "INGREDIENTS": syntax error (code 1): , while compiling: CREATE INGREDIENTS TABLE IF NOT EXISTS table_ingredients(ing_id INTEGER PRIMARY KEY AUTOINCREMENT,ing_name TEXT,ing_density REAL)
02-01 12:48:43.895: E/AndroidRuntime(7186): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-01 12:48:43.895: E/AndroidRuntime(7186): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
02-01 12:48:43.895: E/AndroidRuntime(7186): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
02-01 12:48:43.895: E/AndroidRuntime(7186): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-01 12:48:43.895: E/AndroidRuntime(7186): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-01 12:48:43.895: E/AndroidRuntime(7186): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-01 12:48:43.895: E/AndroidRuntime(7186): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
02-01 12:48:43.895: E/AndroidRuntime(7186): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
02-01 12:48:43.895: E/AndroidRuntime(7186): at com.sfware.quickbakeconverterpro.DatabaseHelper.onCreate(DatabaseHelper.java:49)
02-01 12:48:43.895: E/AndroidRuntime(7186): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
02-01 12:48:43.895: E/AndroidRuntime(7186): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-01 12:48:43.895: E/AndroidRuntime(7186): at com.sfware.quickbakeconverterpro.DatabaseHelper.open(DatabaseHelper.java:72)
02-01 12:48:43.895: E/AndroidRuntime(7186): at com.sfware.quickbakeconverterpro.QuickBakeConverterPro$LoadAllTask.doInBackground(QuickBakeConverterPro.java:690)
02-01 12:48:43.895: E/AndroidRuntime(7186): at com.sfware.quickbakeconverterpro.QuickBakeConverterPro$LoadAllTask.doInBackground(QuickBakeConverterPro.java:1)
02-01 12:48:43.895: E/AndroidRuntime(7186): at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-01 12:48:43.895: E/AndroidRuntime(7186): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-01 12:48:43.895: E/AndroidRuntime(7186): ... 5 more
02-01 12:48:44.045: D/libEGL(7186): loaded /system/lib/egl/libEGL_mali.so
02-01 12:48:44.135: D/libEGL(7186): loaded /system/lib/egl/libGLESv1_CM_mali.so
02-01 12:48:44.145: D/libEGL(7186): loaded /system/lib/egl/libGLESv2_mali.so
02-01 12:48:44.215: D/OpenGLRenderer(7186): Enabling debug mode 0
02-01 12:48:44.400: D/QBCPro:::(7186): Entering onPause()...
02-01 12:48:44.405: D/QBCPro:::(7186): Leaving onPause()...
02-01 12:48:44.545: D/QBCPro:::(7186): Entering onStop()...
02-01 12:48:44.545: D/QBCPro:::(7186): About to save data in new SaveAllTask
02-01 12:48:44.545: D/QBCPro:::(7186): Leaving onStop()...
02-01 12:48:44.545: D/QBCPro:::(7186): SaveAllTask doInBackground called...
02-01 12:48:44.560: E/SQLiteLog(7186): (1) near "INGREDIENTS": syntax error

谁能帮我找出来?不幸的是,我仍然对 SQLiteDatabase 语法生疏。

最佳答案

快速修复:

改为

 String CREATE_ING_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_INGREDIENTS + "("
+ ING_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + ING_KEY_NAME + " TEXT,"
+ ING_KEY_DENSITY + " REAL" + ")";

说明:

execSQL() 方法需要一个 SQL 字符串,它必须按照指南以特定方式构造。原题有CREATEINGREDIENTSTABLE IF NOT EXISTSexecSQL() 方法不支持。当它被替换为 CREATE TABLE IF NOT EXISTS 时,它将正常工作。

有关更多信息,请查看 SQLiteDatabase page在 developer.android.com 上,特别是两个 execSQL() 方法。

关于Android SQLiteDatabase 语法错误,代码 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14644894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com