连接到数据库时出现SQlite错误(SQlite error when connecting to database)

我是Android的新手,在使用SQlite数据库的应用程序上工作。 不幸的是,应用程序总是在应该从数据库中检索数据时崩溃。 我找不到错误,所以任何提示或帮助都会非常感激。

这是活动:

import android.app.ListActivity; import android.content.DialogInterface; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.widget.ListAdapter; import android.widget.SimpleCursorAdapter; import java.util.ArrayList; import de.die_web_agenten.www.runinstant.db.TaskContract; import de.die_web_agenten.www.runinstant.db.TaskDBHelper; public class SecondListActivity extends ListActivity { // declare class variables private ArrayList<Item> m_parts = new ArrayList<Item>(); private Runnable viewParts; private ItemAdapter m_adapter; private ListAdapter listAdapter; private TaskDBHelper helper; /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // instantiate our ItemAdapter class m_adapter = new ItemAdapter(this, R.layout.list_item, m_parts); setListAdapter(m_adapter); // here we are defining our runnable thread. viewParts = new Runnable(){ public void run(){ handler.sendEmptyMessage(0); } }; // here we call the thread we just defined - it is sent to the handler below. Thread thread = new Thread(null, viewParts, "MagentoBackground"); thread.start(); } private void updateUI() { helper = new TaskDBHelper(SecondListActivity.this); SQLiteDatabase sqlDB = helper.getReadableDatabase(); Cursor cursor = sqlDB.query(TaskContract.TABLE, new String[]{TaskContract.Columns._id, TaskContract.Columns.SCAN_RESULT, TaskContract.Columns.SCAN_RESULT_FORMAT }, 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 onClick(DialogInterface dialogInterface, int i, Cursor cursor) { cursor.moveToLast(); int keyIdOfRowToDelete = cursor.getInt(cursor.getColumnIndex(TaskContract.Columns._id)); SQLiteDatabase sqlDB = null; sqlDB.delete(TaskContract.TABLE, TaskContract.Columns._id + "=?", new String[]{String.valueOf(keyIdOfRowToDelete)}); String sql = String.format("SELECT FROM %s WHERE %s = '%s'", TaskContract.TABLE, TaskContract.Columns.SCAN_RESULT, TaskContract.Columns.SCAN_RESULT_FORMAT); //task); //TaskContract.Columns.DESCRIPTION, //description); helper = new TaskDBHelper(SecondListActivity.this); sqlDB = helper.getWritableDatabase(); sqlDB.execSQL(sql); updateUI(); } private Handler handler = new Handler() { public void handleMessage(Message msg) { // create some objects // here is where you could also request data from a server // and then create objects from that data. helper = new TaskDBHelper(SecondListActivity.this); SQLiteDatabase sqlDB = helper.getWritableDatabase(); String sql = String.format("SELECT FROM %s", TaskContract.TABLE, TaskContract.Columns.SCAN_RESULT_FORMAT, TaskContract.Columns.SCAN_RESULT ); sqlDB.execSQL(sql); //updateUI(); String contents = TaskContract.Columns.SCAN_RESULT; String format = TaskContract.Columns.SCAN_RESULT_FORMAT; //Intent SecondIntent = getIntent(); //String contents = SecondIntent.getStringExtra("SCAN_RESULT"); //String contents = SecondIntent.getStringExtra("SCAN_RESULT", contents); //String format = SecondIntent.getStringExtra("SCAN_RESULT_FORMAT"); //Intent intent = new Intent(); //String contents = intent.getStringExtra("SCAN_RESULT"); //String format = intent.getStringExtra("SCAN_RESULT_FORMAT"); //setResult(RESULT_OK, intent); m_parts.add(new Item(contents, format, 0)); m_parts.add(new Item("MyItemName #2", "This is item #2", 0)); m_parts.add(new Item(contents, format, 0)); Log.d("CDebugTag", "Value: " + (contents)); Log.d("DDebugTag", "Value: " + (format)); //updateUI(); //Log.d("DDebugTag", "Value: " + (format)); /*m_parts.add(new Item("MyItemName", "This is item #3", 0)); m_parts.add(new Item("MyItemName #2", "This is item #4", 0)); m_parts.add(new Item("MyItemName", "This is item #5", 0)); m_parts.add(new Item("MyItemName #2", "This is item #6", 0)); m_parts.add(new Item("MyItemName", "This is item #7", 0)); m_parts.add(new Item("MyItemName #2", "This is item #8", 0)); m_parts.add(new Item("MyItemName", "This is item #9", 0)); m_parts.add(new Item("MyItemName #2", "This is item #10", 0));*/ m_adapter = new ItemAdapter(SecondListActivity.this, R.layout.list_item, m_parts); // display the list. setListAdapter(m_adapter); //updateUI(); } }; }

这是Log Cat错误消息:

08-19 17:19:53.270 11362-11362/? E/AndroidRuntime: FATAL EXCEPTION: main Process: de.die_web_agenten.www.runinstant, PID: 11362 android.database.sqlite.SQLiteException: near "FROM": syntax error (code 1): , while compiling: SELECT FROM SCANDATA ################################################################# Error Code : 1 (SQLITE_ERROR) Caused By : SQL(query) error or missing database. (near "FROM": syntax error (code 1): , while compiling: SELECT FROM SCANDATA) ################################################################# 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.java:1812) at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1743) at de.die_web_agenten.www.runinstant.SecondListActivity$2.handleMessage(SecondListActivity.java:111) 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)

任何提示和帮助将不胜感激,谢谢!

I am new to Android and working on an App with a SQlite database. Unfortunately, the app always crashes when it should retrieve data from a database. I cannot find the error, so any hints or help would be very much appreciated.

Here´s the activity:

import android.app.ListActivity; import android.content.DialogInterface; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.widget.ListAdapter; import android.widget.SimpleCursorAdapter; import java.util.ArrayList; import de.die_web_agenten.www.runinstant.db.TaskContract; import de.die_web_agenten.www.runinstant.db.TaskDBHelper; public class SecondListActivity extends ListActivity { // declare class variables private ArrayList<Item> m_parts = new ArrayList<Item>(); private Runnable viewParts; private ItemAdapter m_adapter; private ListAdapter listAdapter; private TaskDBHelper helper; /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // instantiate our ItemAdapter class m_adapter = new ItemAdapter(this, R.layout.list_item, m_parts); setListAdapter(m_adapter); // here we are defining our runnable thread. viewParts = new Runnable(){ public void run(){ handler.sendEmptyMessage(0); } }; // here we call the thread we just defined - it is sent to the handler below. Thread thread = new Thread(null, viewParts, "MagentoBackground"); thread.start(); } private void updateUI() { helper = new TaskDBHelper(SecondListActivity.this); SQLiteDatabase sqlDB = helper.getReadableDatabase(); Cursor cursor = sqlDB.query(TaskContract.TABLE, new String[]{TaskContract.Columns._id, TaskContract.Columns.SCAN_RESULT, TaskContract.Columns.SCAN_RESULT_FORMAT }, 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 onClick(DialogInterface dialogInterface, int i, Cursor cursor) { cursor.moveToLast(); int keyIdOfRowToDelete = cursor.getInt(cursor.getColumnIndex(TaskContract.Columns._id)); SQLiteDatabase sqlDB = null; sqlDB.delete(TaskContract.TABLE, TaskContract.Columns._id + "=?", new String[]{String.valueOf(keyIdOfRowToDelete)}); String sql = String.format("SELECT FROM %s WHERE %s = '%s'", TaskContract.TABLE, TaskContract.Columns.SCAN_RESULT, TaskContract.Columns.SCAN_RESULT_FORMAT); //task); //TaskContract.Columns.DESCRIPTION, //description); helper = new TaskDBHelper(SecondListActivity.this); sqlDB = helper.getWritableDatabase(); sqlDB.execSQL(sql); updateUI(); } private Handler handler = new Handler() { public void handleMessage(Message msg) { // create some objects // here is where you could also request data from a server // and then create objects from that data. helper = new TaskDBHelper(SecondListActivity.this); SQLiteDatabase sqlDB = helper.getWritableDatabase(); String sql = String.format("SELECT FROM %s", TaskContract.TABLE, TaskContract.Columns.SCAN_RESULT_FORMAT, TaskContract.Columns.SCAN_RESULT ); sqlDB.execSQL(sql); //updateUI(); String contents = TaskContract.Columns.SCAN_RESULT; String format = TaskContract.Columns.SCAN_RESULT_FORMAT; //Intent SecondIntent = getIntent(); //String contents = SecondIntent.getStringExtra("SCAN_RESULT"); //String contents = SecondIntent.getStringExtra("SCAN_RESULT", contents); //String format = SecondIntent.getStringExtra("SCAN_RESULT_FORMAT"); //Intent intent = new Intent(); //String contents = intent.getStringExtra("SCAN_RESULT"); //String format = intent.getStringExtra("SCAN_RESULT_FORMAT"); //setResult(RESULT_OK, intent); m_parts.add(new Item(contents, format, 0)); m_parts.add(new Item("MyItemName #2", "This is item #2", 0)); m_parts.add(new Item(contents, format, 0)); Log.d("CDebugTag", "Value: " + (contents)); Log.d("DDebugTag", "Value: " + (format)); //updateUI(); //Log.d("DDebugTag", "Value: " + (format)); /*m_parts.add(new Item("MyItemName", "This is item #3", 0)); m_parts.add(new Item("MyItemName #2", "This is item #4", 0)); m_parts.add(new Item("MyItemName", "This is item #5", 0)); m_parts.add(new Item("MyItemName #2", "This is item #6", 0)); m_parts.add(new Item("MyItemName", "This is item #7", 0)); m_parts.add(new Item("MyItemName #2", "This is item #8", 0)); m_parts.add(new Item("MyItemName", "This is item #9", 0)); m_parts.add(new Item("MyItemName #2", "This is item #10", 0));*/ m_adapter = new ItemAdapter(SecondListActivity.this, R.layout.list_item, m_parts); // display the list. setListAdapter(m_adapter); //updateUI(); } }; }

Here´s the Log Cat error message:

08-19 17:19:53.270 11362-11362/? E/AndroidRuntime: FATAL EXCEPTION: main Process: de.die_web_agenten.www.runinstant, PID: 11362 android.database.sqlite.SQLiteException: near "FROM": syntax error (code 1): , while compiling: SELECT FROM SCANDATA ################################################################# Error Code : 1 (SQLITE_ERROR) Caused By : SQL(query) error or missing database. (near "FROM": syntax error (code 1): , while compiling: SELECT FROM SCANDATA) ################################################################# 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.java:1812) at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1743) at de.die_web_agenten.www.runinstant.SecondListActivity$2.handleMessage(SecondListActivity.java:111) 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)

Any hints and help would be appreciated, Thanks!

最满意答案

您的查询未指定所选内容,例如

SELECT [column list] FROM SCANDATA

要么

SELECT * FROM SCANDATA

Your query doesn't specify what is selected, e.g.

SELECT [column list] FROM SCANDATA

or

SELECT * FROM SCANDATA

更多推荐