Java 更新SQLite数据库导致错误
我是Android新手,正在开发一个应用程序,该应用程序可以从相机中检索数据并将其存储到SQlite数据库中。不幸的是,数据库更新不起作用,因此任何提示或评论都将不胜感激 代码如下:Java 更新SQLite数据库导致错误,java,android,sqlite,Java,Android,Sqlite,我是Android新手,正在开发一个应用程序,该应用程序可以从相机中检索数据并将其存储到SQlite数据库中。不幸的是,数据库更新不起作用,因此任何提示或评论都将不胜感激 代码如下: package de.die_web_agenten.www.runinstant; import android.app.Activity; import android.app.AlertDialog; import android.content.ActivityNotFoundException; imp
package de.die_web_agenten.www.runinstant;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
import de.die_web_agenten.www.runinstant.db.TaskContract;
import de.die_web_agenten.www.runinstant.db.TaskDBHelper;
public class AndroidBarcodeQrExample extends Activity {
/** Called when the activity is first created. */
static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";
private ListAdapter listAdapter;
private TaskDBHelper helper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_barcode);
}
public void scanBar(View v) {
try {
Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
startActivityForResult(intent, 0);
} catch (ActivityNotFoundException anfe) {
showDialog(AndroidBarcodeQrExample.this, "No Scanner Found", "Download a scanner code activity?", "Yes", "No").show();
}
}
public void scanQR(View v) {
try {
Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
} catch (ActivityNotFoundException anfe) {
showDialog(AndroidBarcodeQrExample.this, "No Scanner Found", "Download a scanner code activity?", "Yes", "No").show();
}
}
private static AlertDialog showDialog(final Activity act, CharSequence title, CharSequence message, CharSequence buttonYes, CharSequence buttonNo) {
AlertDialog.Builder downloadDialog = new AlertDialog.Builder(act);
downloadDialog.setTitle(title);
downloadDialog.setMessage(message);
downloadDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
try {
act.startActivity(intent);
} catch (ActivityNotFoundException anfe) {
}
}
});
downloadDialog.setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
}
});
return downloadDialog.show();
}
private void updateUI() {
helper = new TaskDBHelper(AndroidBarcodeQrExample.this);
SQLiteDatabase sqlDB = helper.getReadableDatabase();
Cursor cursor = sqlDB.query(TaskContract.TABLE,
new String[]{ TaskContract.Columns.SCAN_RESULT, TaskContract.Columns.SCAN_RESULT_FORMAT,TaskContract.Columns._id,
TaskContract.Columns.DATE},
null, null, null, null, null
);
listAdapter = new SimpleCursorAdapter(
this,
R.layout.task_view,
cursor,
new String[]{TaskContract.Columns.SCAN_RESULT_FORMAT, TaskContract.Columns.SCAN_RESULT, TaskContract.Columns._id},
new int[]{R.id.taskTextView},
0
);
this.setListAdapter(listAdapter);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
helper = new TaskDBHelper(AndroidBarcodeQrExample.this);
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
//values.clear();
values.put(TaskContract.Columns.SCAN_RESULT, contents);
values.put(TaskContract.Columns.SCAN_RESULT_FORMAT, format);
db.insertWithOnConflict(TaskContract.TABLE, null, values, SQLiteDatabase.CONFLICT_IGNORE);
//Toast toast = Toast.makeText(this, "Content:" + contents + " Format:" + format, Toast.LENGTH_LONG);
//toast.show();
/*Intent SecondIntent = new Intent(AndroidBarcodeQrExample.this, SecondListActivity.class);
SecondIntent.putExtra("SCAN_RESULT", contents);
startActivity(SecondIntent);*/
//Intent SecondIntent = new Intent(getBaseContext(), SecondListActivity.class);
//intent.putExtra("SCAN_RESULT", contents);
//intent.putExtra("SCAN_RESULT_FORMAT", format);
//Intent i = new Intent(this, SecondListActivity.class);
//startActivityForResult(i, 1);
//startActivity(SecondIntent);
Log.d("ADebugTag", "Value: " + (contents));
Log.d("BDebugTag", "Value: " + (format));
//updateUI();
//Context context = getApplicationContext();
//CharSequence text = "Informationen erfolgreich gespeichert!";
//int duration = Toast.LENGTH_SHORT;
//Toast toast = Toast.makeText(context, text, duration);
}
}
}
public void setListAdapter(ListAdapter listAdapter) {
this.listAdapter = listAdapter;
}
}
下面是TaskDBHelper类:
package de.die_web_agenten.www.runinstant.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class TaskDBHelper extends SQLiteOpenHelper {
public TaskDBHelper(Context context) {
super(context, TaskContract.DB_NAME, null, TaskContract.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqlDB) {
String sqlQuery =
String.format("CREATE TABLE %s (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "%s TEXT, %s TEXT)",
TaskContract.TABLE,
TaskContract.Columns.SCAN_RESULT,
TaskContract.Columns.SCAN_RESULT_FORMAT
);
Log.d("TaskDBHelper", "Query to form table: " + sqlQuery);
sqlDB.execSQL(sqlQuery);
}
@Override
public void onUpgrade(SQLiteDatabase sqlDB, int i, int i2) {
sqlDB.execSQL("DROP TABLE IF EXISTS "+TaskContract.TABLE);
onCreate(sqlDB);
}
}
以下是TaskContract类:
package de.die_web_agenten.www.runinstant.db;
public class TaskContract {
public static final String DB_NAME = "de.die_web_agenten.www.runinstant";
public static final int DB_VERSION = 1;
public static final String TABLE = "VALUES";
public static class Columns {
public static final String SCAN_RESULT = "SCAN_RESULT";
public static final String SCAN_RESULT_FORMAT = "SCAN_RESULT_FORMAT";
//public static final String DATE = "date";
public static final String _id = "_id";
}
}
Here´s the TaskDBHelper class:
package de.die_web_agenten.www.runinstant.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class TaskDBHelper extends SQLiteOpenHelper {
public TaskDBHelper(Context context) {
super(context, TaskContract.DB_NAME, null, TaskContract.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqlDB) {
String sqlQuery =
String.format("CREATE TABLE %s (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "%s TEXT, %s TEXT)",
TaskContract.TABLE,
TaskContract.Columns.SCAN_RESULT,
TaskContract.Columns.SCAN_RESULT_FORMAT
);
Log.d("TaskDBHelper", "Query to form table: " + sqlQuery);
sqlDB.execSQL(sqlQuery);
}
@Override
public void onUpgrade(SQLiteDatabase sqlDB, int i, int i2) {
sqlDB.execSQL("DROP TABLE IF EXISTS "+TaskContract.TABLE);
onCreate(sqlDB);
}
}
下面是日志:
08-15 16:55:12.356 15118-15118/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.die_web_agenten.www.runinstant, PID: 15118
java.lang.RuntimeException: Unable to resume activity {de.die_web_agenten.www.runinstant/de.die_web_agenten.www.runinstant.AndroidBarcodeQrExample}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=com.google.zxing.client.android.SCAN flg=0x80000 (has extras) }} to activity {de.die_web_agenten.www.runinstant/de.die_web_agenten.www.runinstant.AndroidBarcodeQrExample}: android.database.sqlite.SQLiteException: near "VALUES": syntax error (code 1): , while compiling: CREATE TABLE VALUES (_id INTEGER PRIMARY KEY AUTOINCREMENT, SCAN_RESULT TEXT, SCAN_RESULT_FORMAT TEXT)
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(near "VALUES": syntax error (code 1): , while compiling: CREATE TABLE VALUES (_id INTEGER PRIMARY KEY AUTOINCREMENT, SCAN_RESULT TEXT, SCAN_RESULT_FORMAT TEXT))
#################################################################
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3409)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3440)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2774)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
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:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=com.google.zxing.client.android.SCAN flg=0x80000 (has extras) }} to activity {de.die_web_agenten.www.runinstant/de.die_web_agenten.www.runinstant.AndroidBarcodeQrExample}: android.database.sqlite.SQLiteException: near "VALUES": syntax error (code 1): , while compiling: CREATE TABLE VALUES (_id INTEGER PRIMARY KEY AUTOINCREMENT, SCAN_RESULT TEXT, SCAN_RESULT_FORMAT TEXT)
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(near "VALUES": syntax error (code 1): , while compiling: CREATE TABLE VALUES (_id INTEGER PRIMARY KEY AUTOINCREMENT, SCAN_RESULT TEXT, SCAN_RESULT_FORMAT TEXT))
#################################################################
at android.app.ActivityThread.deliverResults(ActivityThread.java:4005)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3395)
... 11 more
Caused by: android.database.sqlite.SQLiteException: near "VALUES": syntax error (code 1): , while compiling: CREATE TABLE VALUES (_id INTEGER PRIMARY KEY AUTOINCREMENT, SCAN_RESULT TEXT, SCAN_RESULT_FORMAT TEXT)
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(near "VALUES": syntax error (code 1): , while compiling: CREATE TABLE VALUES (_id INTEGER PRIMARY KEY AUTOINCREMENT, SCAN_RESULT TEXT, SCAN_RESULT_FORMAT TEXT))
#################################################################
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.
08-15 16:55:12.35615118-15118/?E/AndroidRuntime:致命异常:主
流程:de.die_web_agenten.www.runinstant,PID:15118
java.lang.RuntimeException:无法恢复活动{de.die_web_agenten.www.runinstant/de.die_web_agenten.www.runinstant.AndroidBarcodeQrExample}:java.lang.RuntimeException:无法将结果结果信息{who=null,request=0,result=-1,data=Intent{act=com.google.zxing.client.android.SCAN flg=0x80000(有附加项)}传递到活动{de.die_web_agenten.www.runinstant/de.die_web_agenten.www.runinstant.AndroidBarcodeQrExample}:android.database.sqlite.SQLiteException:near“VALUES”:语法错误(代码1):,编译时:创建表值(_id整型主键自动递增,扫描结果文本,扫描结果格式文本)
#################################################################
错误代码:1(SQLITE_错误)
原因:SQL(查询)错误或缺少数据库。
(靠近“值”:语法错误(代码1):,编译时:创建表值(_idinteger主键自动递增,扫描结果文本,扫描结果格式文本))
#################################################################
位于android.app.ActivityThread.performResumeActivity(ActivityThread.java:3409)
位于android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3440)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2774)上
在android.app.ActivityThread.access$900(ActivityThread.java:177)
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)
位于android.os.Handler.dispatchMessage(Handler.java:102)
位于android.os.Looper.loop(Looper.java:135)
位于android.app.ActivityThread.main(ActivityThread.java:5910)
位于java.lang.reflect.Method.invoke(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)上
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
原因:java.lang.RuntimeException:未能将结果ResultInfo{who=null,request=0,result=-1,data=Intent{act=com.google.zxing.client.android.SCAN flg=0x80000(有附加项)}}传递到活动{de.die_web_agenten.www.runinstant/de.die_web_agenten.www.runinstant.AndroidBarcodeQrExample}:android.database.sqlite.SQLiteException:near“值”:语法错误(代码1):,编译时:创建表值(_idinteger主键自动递增、扫描结果文本、扫描结果格式文本)
#################################################################
错误代码:1(SQLITE_错误)
原因:SQL(查询)错误或缺少数据库。
(靠近“值”:语法错误(代码1):,编译时:创建表值(_idinteger主键自动递增,扫描结果文本,扫描结果格式文本))
#################################################################
位于android.app.ActivityThread.deliverResults(ActivityThread.java:4005)
位于android.app.ActivityThread.performResumeActivity(ActivityThread.java:3395)
…还有11个
原因:android.database.sqlite.SQLiteException:靠近“值”:语法错误(代码1),编译时:创建表值(\u id INTEGER主键自动递增,扫描结果文本,扫描结果格式文本)
#################################################################
错误代码:1(SQLITE_错误)
原因:SQL(查询)错误或缺少数据库。
(靠近“值”:语法错误(代码1):,编译时:创建表值(_idinteger主键自动递增,扫描结果文本,扫描结果格式文本))
#################################################################
位于android.database.sqlite.SQLiteConnection.nativePrepareStatement(本机方法)
位于android.database.sqlite.SQLiteConnection.acquirePrepa
String sqlQuery =
String.format("CREATE TABLE %s (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"%s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT)",
TaskContract.TABLE,
TaskContract.Columns.SCAN_RESULT,
TaskContract.Columns.SCAN_RESULT_FORMAT
// 2 more to go!!
);
"CREATE TABLE %s (_id INTEGER PRIMARY KEY AUTOINCREMENT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT)"
TaskContract.TABLE, TaskContract.Columns.SCAN_RESULT, TaskContract.Columns.SCAN_RESULT_FORMAT