我收到了这个java运行时异常,我也检查了我的查询。一切似乎都是正确的。我只是不明白我哪里出错了 我检查了它,并发现任何错误。请帮我 这是我的logcat
07-05 10:59:38.204 12209-12209/com.example.user.easytodolist E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.easytodolist, PID: 12209
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.easytodolist/com.example.user.easytodolist.MainActivity}: android.database.sqlite.SQLiteException: near "TABLEtasks": syntax error (code 1): , while compiling: CREATE TABLEtasks(_idINTEGER PRIMARY KEY AUTOINCREMENT, titleTEXT NOT NULL);
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(near "TABLEtasks": syntax error (code 1): , while compiling: CREATE TABLEtasks(_idINTEGER PRIMARY KEY AUTOINCREMENT, titleTEXT NOT NULL);)
#################################################################
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7331)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.database.sqlite.SQLiteException: near "TABLEtasks": syntax error (code 1): , while compiling: CREATE TABLEtasks(_idINTEGER PRIMARY KEY AUTOINCREMENT, titleTEXT NOT NULL);
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(near "TABLEtasks": syntax error (code 1): , while compiling: CREATE TABLEtasks(_idINTEGER PRIMARY KEY AUTOINCREMENT, titleTEXT NOT NULL);)
#################################################################
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1000)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:565)
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:1838)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1769)
at com.example.user.easytodolist.db.TaskHelper.onCreate(TaskHelper.java:17)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
at com.example.user.easytodolist.MainActivity.updateUI(MainActivity.java:84)
at com.example.user.easytodolist.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:6904)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
... 9 more
这是我的 MainActivity 。
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.preference.DialogPreference;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.example.user.easytodolist.db.Task;
import com.example.user.easytodolist.db.TaskHelper;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private TaskHelper mHelper;
private ListView mTaskListView;
private ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHelper = new TaskHelper(this);
mTaskListView = (ListView) findViewById(R.id.list_todo);
updateUI();
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main_menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.action_add_task:
final EditText taskEditTask = new EditText(this);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("New Task")
.setMessage("Add a New Task")
.setView(taskEditTask)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String task = String.valueOf(taskEditTask.getText());
SQLiteDatabase db = mHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(com.example.user.easytodolist.db.Task.TaskEntry.COL_TASK_TITLE,task);
db.insertWithOnConflict(Task.TaskEntry.TABLE,null,values,SQLiteDatabase.CONFLICT_REPLACE);
db.close();
updateUI();
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void updateUI() {
ArrayList<String> taskList = new ArrayList<>();
SQLiteDatabase db = mHelper.getReadableDatabase();
Cursor cursor = db.query(Task.TaskEntry.TABLE,
new String[]{Task.TaskEntry._ID, Task.TaskEntry.COL_TASK_TITLE} ,null,null,null,null,null);
while (cursor.moveToNext()){
int index = cursor.getColumnIndex(Task.TaskEntry.COL_TASK_TITLE);
taskList.add(cursor.getString(index));
}
if(mAdapter == null){
mAdapter = new ArrayAdapter<String>(this, R.layout.item_todo,R.id.task_title,taskList);
mTaskListView.setAdapter(mAdapter);
} else{
mAdapter.clear();
mAdapter.addAll(taskList);
mAdapter.notifyDataSetChanged();
}
cursor.close();
db.close();
}
public void deleteTask(View view){
View parent = (View) view.getParent();
TextView taskTextView = (TextView) parent.findViewById(R.id.task_title);
String task = String.valueOf(taskTextView.getText());
SQLiteDatabase db = mHelper.getWritableDatabase();
db.delete(Task.TaskEntry.TABLE, Task.TaskEntry.COL_TASK_TITLE + "= ?", new String[] {task});
db.close();
updateUI();
}
}
我的TaskHelperclass
package com.example.user.easytodolist.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class TaskHelper extends SQLiteOpenHelper {
public TaskHelper(Context context) {
super(context,Task.DB_NAME,null,Task.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + Task.TaskEntry.TABLE + " ( " +
Task.TaskEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
Task.TaskEntry.COL_TASK_TITLE + " TEXT NOT NULL);";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + Task.TaskEntry.TABLE);
onCreate(db);
}
}
和任务类
package com.example.user.easytodolist.db;
import android.provider.BaseColumns;
public class Task {
public static final String DB_NAME = "com.example.user.easytodolist.db";
public static final int DB_VERSION = 1;
public class TaskEntry implements BaseColumns{
public static final String TABLE= "tasks";
public static final String COL_TASK_TITLE = "title";
}
}
答案 0 :(得分:3)
CREATE TABLEtasks(_idINTEGER PRIMARY KEY AUTOINCREMENT, titleTEXT NOT NULL);
此查询应为
CREATE TABLE tasks(id INTEGER PRIMARY KEY, title TEXT NOT NULL);
你错过了空间