在我的Android APP中,我试图从一个表中获取详细信息,然后将这些记录插入到另一个表中并加入另一个表。
SQLLITE插入语句连续多次失败。我已经尝试了很多东西来解决INSERT语句中的问题,但我没有成功。
我已经附加了日志,SQL表声明和我面临问题的INSERT语句。你能帮我解决一下这个问题。
sql = "INSERT INTO " + DATABASE_TABLE_TRAN + " (" +
KEY_ID + "," +
KEY_NAME + "," +
KEY_COIN + "," +
KEY_DATE + "," +
KEY_TYPE + "," +
KEY_QTY + "," +
KEY_PRICE + "," +
KEY_PRICE_UNIT +
") " +
" VALUES (" +
" SELECT " +
"TRN." +KEY_ID + "," +
"ALT." +KEY_NAME + "," +
"TRN." +KEY_COIN + "," +
"TRN." +KEY_DATE + "," +
"TRN." +KEY_TYPE + "," +
"TRN." +KEY_QTY + "," +
"TRN." +KEY_PRICE + "," +
"TRN." +KEY_PRICE_UNIT +
" FROM " + DATABASE_TABLE_TRAN_TEMP + " TRN, " +
DATABASE_TABLE_ALTCOIN + " ALT " +
" WHERE " + "TRN."+KEY_COIN + " = " + "ALT."+KEY_COIN +
" )";
Log.i("Insert Query",sql);
db.execSQL(sql);
logcat的
12-21 09:35:30.776 5901-5901/? I/Insert Query: INSERT INTO TB_TRAN (ID,NAME,COIN,DATE,TYPE,QTY,PRICE,PRICE_UNIT) VALUES ( SELECT TRN.ID,ALT.NAME,TRN.COIN,TRN.DATE,TRN.TYPE,TRN.QTY,TRN.PRICE,TRN.PRICE_UNIT FROM TB_TRAN_TEMP TRN, TB_ALTCOIN ALT WHERE TRN.COIN = ALT.COIN )
12-21 09:35:30.776 5901-5901/? E/SQLiteLog: (1) near "SELECT": syntax error
12-21 09:35:30.792 5901-5901/? D/AndroidRuntime: Shutting down VM
12-21 09:35:30.794 5901-5901/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xx.xx.xx, PID: 5901
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xx.xx.xx/com.xx.xx.xx.AndroidLauncher}: android.database.sqlite.SQLiteException: near "SELECT": syntax error (code 1): , while compiling: INSERT INTO TB_TRAN (ID,NAME,COIN,DATE,TYPE,QTY,PRICE,PRICE_UNIT) VALUES ( SELECT TRN.ID,ALT.NAME,TRN.COIN,TRN.DATE,TRN.TYPE,TRN.QTY,TRN.PRICE,TRN.PRICE_UNIT FROM TB_TRAN_TEMP TRN, TB_ALTCOIN ALT WHERE TRN.COIN = ALT.COIN )
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2320)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2380)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5291)
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)
Caused by: android.database.sqlite.SQLiteException: near "SELECT": syntax error (code 1): , while compiling: INSERT INTO TB_TRAN (ID,NAME,COIN,DATE,TYPE,QTY,PRICE,PRICE_UNIT) VALUES ( SELECT TRN.ID,ALT.NAME,TRN.COIN,TRN.DATE,TRN.TYPE,TRN.QTY,TRN.PRICE,TRN.PRICE_UNIT FROM TB_TRAN_TEMP TRN, TB_ALTCOIN ALT WHERE TRN.COIN = ALT.COIN )
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.executeSql(SQLiteDatabase.java:1676)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1607)
at a.a$a.onUpgrade(SourceFile:453)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:256)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at a.a.a(SourceFile:495)
at com.xx.xx.xx.AndroidLauncher.s(SourceFile:1252)
at com.xx.xx.xx.AndroidLauncher.g(SourceFile:1727)
at com.xx.xx.xx.AndroidLauncher.onCreate(SourceFile:240)
at android.app.Activity.performCreate(Activity.java:6018)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2273)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2380)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5291)
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)
表格声明
String DATABASE_TABLE_TRAN_TEMP = "TB_TRAN_TEMP";
sql="CREATE TABLE " +
DATABASE_TABLE_TRAN_TEMP +
"(" +
KEY_TRAN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT , " +
KEY_ID + " TEXT NOT NULL, " +
KEY_COIN + " TEXT NOT NULL, " +
KEY_DATE + " TEXT NOT NULL, " +
KEY_TYPE + " TEXT NOT NULL, " +
KEY_QTY + " DECIMAL(19, 2) NOT NULL, " +
KEY_PRICE + " DECIMAL(19, 2) NOT NULL, " +
KEY_PRICE_UNIT + " TEXT NOT NULL " +
");";
//Log.i("Create Query",sql);
db.execSQL(sql);
String DATABASE_TABLE_TRAN = "TB_TRAN";
sql="CREATE TABLE " +
DATABASE_TABLE_TRAN +
"(" +
KEY_TRAN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT , " +
KEY_ID + " TEXT NOT NULL, " +
KEY_NAME + " TEXT NOT NULL, " + /* added in version 2 */
KEY_COIN + " TEXT NOT NULL, " +
KEY_DATE + " TEXT NOT NULL, " +
KEY_TYPE + " TEXT NOT NULL, " +
KEY_QTY + " DECIMAL(19, 2) NOT NULL, " +
KEY_PRICE + " DECIMAL(19, 2) NOT NULL, " +
KEY_PRICE_UNIT + " TEXT NOT NULL " +
");";
//Log.i("Create Query",sql);
db.execSQL(sql);
sql="CREATE TABLE " +
DATABASE_TABLE_ALTCOIN +
"(" +
KEY_ID + " TEXT PRIMARY KEY, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_COIN + " TEXT NOT NULL, " +
KEY_RANK + " INTEGER NOT NULL, " +
KEY_PRICE + " DECIMAL(19, 2) NOT NULL, " +
KEY_PRICE_UNIT + " TEXT NOT NULL, " +
KEY_PRICE_BTC + " VARCHAR(15) NOT NULL, " +
KEY_PERC_CHANGE_1H + " DECIMAL(5, 2) NOT NULL, " +
KEY_PERC_CHANGE_24H + " DECIMAL(5, 2) NOT NULL, " +
KEY_PERC_CHANGE_7D + " DECIMAL(5, 2) NOT NULL, " +
KEY_VOL_24HR + " VARCHAR(15) NOT NULL, " +
KEY_MKT_CAP + " VARCHAR(15) NOT NULL, " +
KEY_FAV_TEMP + " INTEGER DEFAULT 0 " +
");";
//Log.i("Create Query",sql);
db.execSQL(sql);
答案 0 :(得分:3)
使用VALUES
语句时,不能有insert into ... select ...
个关键字。
删除
" VALUES (" +
和
" )";
来自你的代码。
应该是:
sql = "INSERT INTO " + DATABASE_TABLE_TRAN + " (" +
KEY_ID + "," +
KEY_NAME + "," +
KEY_COIN + "," +
KEY_DATE + "," +
KEY_TYPE + "," +
KEY_QTY + "," +
KEY_PRICE + "," +
KEY_PRICE_UNIT +
") " +
" SELECT " +
"TRN." +KEY_ID + "," +
"ALT." +KEY_NAME + "," +
"TRN." +KEY_COIN + "," +
"TRN." +KEY_DATE + "," +
"TRN." +KEY_TYPE + "," +
"TRN." +KEY_QTY + "," +
"TRN." +KEY_PRICE + "," +
"TRN." +KEY_PRICE_UNIT +
" FROM " + DATABASE_TABLE_TRAN_TEMP + " TRN, " +
DATABASE_TABLE_ALTCOIN + " ALT " +
" WHERE " + "TRN."+KEY_COIN + " = " + "ALT." + KEY_COIN;