【发布时间】:2014-04-04 13:07:33
【问题描述】:
我正在尝试使用 AsyncTasks 插入我的数据库。 当我打电话时:
new insert(tableName).execute(val).get;
它按预期工作,但它阻塞了 UI 线程。
我宁愿打电话
new insert(tableName).execute(val);
但是当我这样做时,doInBackground 会引发 SQL 错误,并且我传入的 ContentValues 是空的。 这是我的 AsyncTask:
private class insert extends AsyncTask<ContentValues, Object, Long>{
String inTable;
long id = -1;
insert(TableNames table){
inTable = table.getSQL(); //This simple gets the String value TableName
}
@Override
protected void onPostExecute(Long result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.i(tag, "PostExecute: "+ inTable + " " + String.valueOf(id));
}
@Override
protected Long doInBackground(ContentValues... values) {
try {
id = getWritableDatabase().insert(inTable, null, values[0]);
} catch (SQLException e) {
e.printStackTrace();
}
return id;
}
}
在这种情况下,我不关心他的返回值。插入物是火,忘记它。
原木猫:
Error inserting
android.database.sqlite.SQLiteException: near "null": syntax error (code 1): , while compiling: INSERT INTO TrackingLog(null) VALUES (NULL)
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.insertWithOnConflict(SQLiteDatabase.java:1467)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
at com.westat.nopus.util.data.DataHelper$insert.doInBackground(DataHelper.java:563)
at com.westat.nopus.util.data.DataHelper$insert.doInBackground(DataHelper.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Error inserting
android.database.sqlite.SQLiteException: near "null": syntax error (code 1): , while compiling: INSERT INTO TrackingLog(null) VALUES (NULL)
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.insertWithOnConflict(SQLiteDatabase.java:1467)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
at com.westat.nopus.util.data.DataHelper$insert.doInBackground(DataHelper.java:563)
at com.westat.nopus.util.data.DataHelper$insert.doInBackground(DataHelper.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
调用AsyncTask的完整方法:
void Tableinsert(TableNames tableName,ContentValues val){
val.put("Exported_Flag", "F");
if (debug) {
Log.i(tag, "TableInsert. Size: " + String.valueOf(val.size()));
//This returns 26 - Which is the correct size of the ContentValues
}
try {
new insert(tableName).execute(val); //Doesn't work
// new insert(tableName).execute(val).get; //Works
}
catch (Exception e) {
if (MyApplication.SEND_HANDLED_EXCEPTIONS) {
ACRA.getErrorReporter().handleSilentException(e);
}
e.printStackTrace();
}
}
【问题讨论】:
-
你是如何调用异步任务的。您必须在调用 asynctask 时传递内容值。
-
@Navaguesser LogCat 中有什么吗??
-
您是在此之后立即完成您的
Activity还是暂停它? -
@Shrimam - 抱歉...我一直按回车键。像这样调用任务: new insert(tableName,false).execute(val);
-
@codeMagic 实际上在这种情况下没有活动。它是从服务中调用的。它是一个 GPS 跟踪日志,仅作为服务运行。但是,从 Activity 插入时我遇到了同样的问题,是的,当您获得调用 asynctask 的按钮时,它就完成了
标签: android sqlite android-asynctask