SQLite插入查询错误

时间:2012-11-22 10:17:51

标签: java android sql sqlite android-sqlite

我正在使用以下查询将数据插入到Android的SQLite数据库中。

INSERT INTO ServerData 
VALUES('404', '192.168.145.101', '1', '1', '1'),
      ('404', '192.168.145.101', '2', '1', '1')

但是我收到错误,LogCat的详细信息如下:

  

11-22 15:14:56.588:E /数据库(1609):在准备' INSERT INTO ServerData VALUES时,在0x2e3e30上失败1(接近",":语法错误)(& #39; 404'' 192.168.145.101'' 1'' 1'' 1&#39),(& #39; 404'' 192.168.145.101'&#39 2'' 1'' 1')&#39 ;。
  11-22 15:14:56.618:W / dalvikvm(1609):threadid = 10:线程退出未捕获异常(组= 0x40015560)
  11-22 15:14:56.648:E / AndroidRuntime(1609):致命异常:AsyncTask#1
  11-22 15:14:56.648:E / AndroidRuntime(1609):java.lang.RuntimeException:执行doInBackground()时发生错误
  11-22 15:14:56.648:E / AndroidRuntime(1609):在android.os.AsyncTask $ 3.done(AsyncTask.java:200)
  11-22 15:14:56.648:E / AndroidRuntime(1609):at java.util.concurrent.FutureTask $ Sync.innerSetException(FutureTask.java:274)   11-22 15:14:56.648:E / AndroidRuntime(1609):at java.util.concurrent.FutureTask.setException(FutureTask.java:125)                 11-22 15:14:56.648:E / AndroidRuntime(1609):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:308)                 11-22 15:14:56.648:E / AndroidRuntime(1609):at java.util.concurrent.FutureTask.run(FutureTask.java:138)                 11-22 15:14:56.648:E / AndroidRuntime(1609):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)                 11-22 15:14:56.648:E / AndroidRuntime(1609):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:581)                 11-22 15:14:56.648:E / AndroidRuntime(1609):at java.lang.Thread.run(Thread.java:1019)                 11-22 15:14:56.648:E / AndroidRuntime(1609):引起:android.database.sqlite.SQLiteException:near",":语法错误:INSERT INTO ServerData VALUES(' 404'' 192.168.145.101'' 1'' 1'' 1&#39),(' 404'' 192.168.145.101'&#39 2'' 1'' 1')                 11-22 15:14:56.648:E / AndroidRuntime(1609):在android.database.sqlite.SQLiteDatabase.native_execSQL(本机方法)                 11-22 15:14:56.648:E / AndroidRuntime(1609):在android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1763)                 11-22 15:14:56.648:E / AndroidRuntime(1609):at com.pcloud.fileshare.database.DBMethods.InsertToServerTable(DBMethods.java:228)                 11-22 15:14:56.648:E / AndroidRuntime(1609):at com.pcloud.fileshare.dataAccess.commonMethod.updateServerTable(commonMethod.java:472)       11-22 15:14:56.648:E / AndroidRuntime(1609):at com.pcloud.fileshare.mainPages.userLogin $ backgroundTask.doInBackground(userLogin.java:238)                 11-22 15:14:56.648:E / AndroidRuntime(1609):at com.pcloud.fileshare.mainPages.userLogin $ backgroundTask.doInBackground(userLogin.java:1)                 11-22 15:14:56.648:E / AndroidRuntime(1609):在android.os.AsyncTask $ 2.call(AsyncTask.java:185)                 11-22 15:14:56.648:E / AndroidRuntime(1609):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:306)                 11-22 15:14:56.648:E / AndroidRuntime(1609):...还有4个                 11-22 15:15:00.059:E / WindowManager(1609):活动com.pcloud.fileshare.mainPages.userLogin已泄露窗口com.android.internal.policy.impl.PhoneWindow$DecorView@40529668最初添加到这里                 11-22 15:15:00.059:E / WindowManager(1609):android.view.WindowLeaked:Activity com.pcloud.fileshare.mainPages.userLogin泄漏了窗口com.android.internal.policy.impl.PhoneWindow$DecorView@40529668这最初是在这里添加的                 11-22 15:15:00.059:E / WindowManager(1609):在android.view.ViewRoot。(ViewRoot.java:258)                 11-22 15:15:00.059:E / WindowManager(1609):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)                 11-22 15:15:00.059:E / WindowManager(1609):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)                 11-22 15:15:00.059:E / WindowManager(1609):在android.view.Window $ LocalWindowManager.addView(Window.java:424)                 11-22 15:15:00.059:E / WindowManager(1609):在android.app.Dialog.show(Dialog.java:241)                 11-22 15:15:00.059:E / WindowManager(1609):at com.pcloud.fileshare.mainPages.userLogin $ backgroundTask.onPreExecute(userLogin.java:251)                 11-22 15:15:00.059:E / WindowManager(1609):在android.os.AsyncTask.execute(AsyncTask.java:391)                 11-22 15:15:00.059:E / WindowManager(1609):at com.pcloud.fileshare.mainPages.userLogin $ 1.onItemClick(userLogin.java:221)                 11-22 15:15:00.059:E / WindowManager(1609):在android.widget.AdapterView.performItemClick(AdapterView.java:284)                 11-22 15:15:00.059:E / WindowManager(1609):在android.widget.ListView.performItemClick(ListView.java:3513)                 11-22 15:15:00.059:E / WindowManager(1609):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:1812)       11-22 15:15:00.059:E / WindowManager(1609):在android.os.Handler.handleCallback(Handler.java:587)                 11-22 15:15:00.059:E / WindowManager(1609):在android.os.Handler.dispatchMessage(Handler.java:92)                 11-22 15:15:00.059:E / WindowManager(1609):在android.os.Looper.loop(Looper.java:123)                 11-22 15:15:00.059:E / WindowManager(1609):在android.app.ActivityThread.main(ActivityThread.java:3683)                 11-22 15:15:00.059:E / WindowManager(1609):at java.lang.reflect.Method.invokeNative(Native Method)                 11-22 15:15:00.059:E / WindowManager(1609):at java.lang.reflect.Method.invoke(Method.java:507)                 11-22 15:15:00.059:E / WindowManager(1609):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)                 11-22 15:15:00.059:E / WindowManager(1609):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)                 11-22 15:15:00.059:E / WindowManager(1609):at dalvik.system.NativeStart.main(Native Method)

相同的查询在SQLite Expert中正常工作,但在代码

中没有

3 个答案:

答案 0 :(得分:1)

检查https://stackoverflow.com/a/5009740/1434631

中的解决方案

您无法使用逗号分隔 sqlite 中插入多行..

最好使用SQLiteDatabase

ContentValues values = new ContentValues();
for(int i = 0; i<=5; i++) {
values.put(COLUMN_NAME, i);
values.put(COLUMN_NAME, 0);
db.insert(TABLE_NAME, null, values);
}

答案 1 :(得分:1)

试试这个 你不能插入用逗号分隔的多个值

INSERT INTO ServerData VALUES('404','192.168.145.101','1','1','1')

 INSERT INTO ServerData VALUES ('404','192.168.145.101','2','1','1')

试试这个

 INSERT INTO 'tablename' ('column1', 'column2') VALUES
    ('data1', 'data2'),
    ('data3', 'data4'),
    ('data5', 'data6'),
   ('data7', 'data8');

可以重命名为sqlite:

 INSERT INTO 'tablename'
  SELECT 'data1' AS 'column1',
         'data2' AS 'column2'
 UNION SELECT 'data3', 'data4'
  UNION SELECT 'data5', 'data6'
  UNION SELECT 'data7', 'data8'

答案 2 :(得分:1)

在SQLite中,您不能使用逗号分隔符在表中插入多行。所以,对于你的情况,你可以试试这个:

INSERT INTO ServerData
      SELECT '404','192.168.145.101','1','1','1'
UNION SELECT '404','192.168.145.101','2','1','1';