所以我正在尝试一个我在互联网上看过的SQLite教程,稍微调整一下后我启动它就崩溃了。在修改代码之前,它可以工作。我知道有些功能已被弃用,但这似乎不是问题,因为正如我所说,它在我修改它之前运行。我也知道错误是在我的一个类的第44行(我在这一行放了一个注释来区分它)但我不明白它为什么会给我一个错误。这是我的代码:
AndroidSQLite.java:
package com.example.sqlite;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class AndroidSQLite extends Activity {
EditText inputContent1, inputContent2, inputContent3;
Button buttonAdd, buttonDeleteAll;
private SQLiteAdapter mySQLiteAdapter;
ListView listContent;
SimpleCursorAdapter cursorAdapter;
Cursor cursor;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inputContent1 = (EditText)findViewById(R.id.UserID);
inputContent2 = (EditText)findViewById(R.id.Password);
inputContent3 = (EditText)findViewById(R.id.Fname);
buttonAdd = (Button)findViewById(R.id.add);
buttonDeleteAll = (Button)findViewById(R.id.deleteall);
listContent = (ListView)findViewById(R.id.contentlist);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();
cursor = mySQLiteAdapter.queueAll();
String[] from = new String[]{SQLiteAdapter.KEY_USERS_USERID, SQLiteAdapter.KEY_USERS_PASSWORD, SQLiteAdapter.KEY_USERS_FNAME};
int[] to = new int[]{R.id.id, R.id.text1, R.id.text2};
cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
buttonAdd.setOnClickListener(buttonAddOnClickListener);
buttonDeleteAll.setOnClickListener(buttonDeleteAllOnClickListener);
}
Button.OnClickListener buttonAddOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String data1 = inputContent1.getText().toString();
String data2 = inputContent2.getText().toString();
String data3 = inputContent3.getText().toString();
mySQLiteAdapter.insert(data1, data2, data3);
updateList();
}
};
Button.OnClickListener buttonDeleteAllOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mySQLiteAdapter.deleteAll();
updateList();
}
};
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mySQLiteAdapter.close();
}
@SuppressWarnings("deprecation")
private void updateList(){
cursor.requery();
}
}
SQLiteAdapter.java
package com.example.sqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class SQLiteAdapter {
public static final String DB_NAME = "adserve";
public static final String DB_TABLE_USERS = "users";
public static final int DB_VERSION = 1;
public static final String KEY_USERS_USERID = "_id";
public static final String KEY_USERS_PASSWORD = "Password";
public static final String KEY_USERS_FNAME = "Fname";
//create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE =
"create table " + DB_TABLE_USERS + " ("
+ KEY_USERS_USERID + " integer primary key, "
+ KEY_USERS_PASSWORD + " text not null, "
+ KEY_USERS_FNAME + " text not null);";
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public SQLiteAdapter(Context c){
context = c;
}
public SQLiteAdapter openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, DB_NAME, null, DB_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public SQLiteAdapter openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, DB_NAME, null, DB_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}
public void close(){
sqLiteHelper.close();
}
public long insert(String UserID, String Password, String Fname){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_USERS_USERID, UserID);
contentValues.put(KEY_USERS_PASSWORD, Password);
contentValues.put(KEY_USERS_FNAME, Fname);
return sqLiteDatabase.insert(DB_TABLE_USERS, null, contentValues);
}
public int deleteAll(){
return sqLiteDatabase.delete(DB_TABLE_USERS, null, null);
}
public Cursor queueAll(){
String[] columns = new String[]{KEY_USERS_USERID, KEY_USERS_PASSWORD, KEY_USERS_FNAME};
Cursor cursor = sqLiteDatabase.query(DB_TABLE_USERS, columns, null, null, null, null, null);
return cursor;
}
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
}
我的logcat:
02-20 10:04:53.811: E/AndroidRuntime(1940): FATAL EXCEPTION: main
02-20 10:04:53.811: E/AndroidRuntime(1940): Process: com.example.sqlite, PID: 1940
02-20 10:04:53.811: E/AndroidRuntime(1940): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqlite/com.example.sqlite.AndroidSQLite}: java.lang.IllegalArgumentException: column '_id' does not exist
02-20 10:04:53.811: E/AndroidRuntime(1940): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
02-20 10:04:53.811: E/AndroidRuntime(1940): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
02-20 10:04:53.811: E/AndroidRuntime(1940): at android.app.ActivityThread.access$700(ActivityThread.java:135)
02-20 10:04:53.811: E/AndroidRuntime(1940): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
02-20 10:04:53.811: E/AndroidRuntime(1940): at android.os.Handler.dispatchMessage(Handler.java:102)
02-20 10:04:53.811: E/AndroidRuntime(1940): at android.os.Looper.loop(Looper.java:137)
02-20 10:04:53.811: E/AndroidRuntime(1940): at android.app.ActivityThread.main(ActivityThread.java:4998)
02-20 10:04:53.811: E/AndroidRuntime(1940): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 10:04:53.811: E/AndroidRuntime(1940): at java.lang.reflect.Method.invoke(Method.java:515)
02-20 10:04:53.811: E/AndroidRuntime(1940): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-20 10:04:53.811: E/AndroidRuntime(1940): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-20 10:04:53.811: E/AndroidRuntime(1940): at dalvik.system.NativeStart.main(Native Method)
02-20 10:04:53.811: E/AndroidRuntime(1940): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
02-20 10:04:53.811: E/AndroidRuntime(1940): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
02-20 10:04:53.811: E/AndroidRuntime(1940): at android.widget.CursorAdapter.init(CursorAdapter.java:172)
02-20 10:04:53.811: E/AndroidRuntime(1940): at android.widget.CursorAdapter.<init>(CursorAdapter.java:120)
02-20 10:04:53.811: E/AndroidRuntime(1940): at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:52)
02-20 10:04:53.811: E/AndroidRuntime(1940): at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:78)
02-20 10:04:53.811: E/AndroidRuntime(1940): at com.example.sqlite.AndroidSQLite.onCreate(AndroidSQLite.java:43)
02-20 10:04:53.811: E/AndroidRuntime(1940): at android.app.Activity.performCreate(Activity.java:5243)
02-20 10:04:53.811: E/AndroidRuntime(1940): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-20 10:04:53.811: E/AndroidRuntime(1940): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
02-20 10:04:53.811: E/AndroidRuntime(1940): ... 11 more
答案 0 :(得分:3)
引用the documentation for CursorAdapter
:
Cursor必须包含名为“_id”的列,否则此类将无效
据我所知,你Cursor
中没有这样的专栏。
一种解决方案是从query()
切换到rawQuery()
,这样您就可以将ROWID AS _id
添加到列列表中,以履行CursorAdapter
合同。
答案 1 :(得分:0)
在SQLite中,表行通常具有64位有符号整数ROWID,它在同一个表中的所有行中都是唯一的。 (没有ROWID表是个例外。)
因此,如果您只想将列用作整数主键,则可以使用ROWID直接访问内置列。
和Cursor adapter上的文件说明了
Cursor必须包含名为“_id”的列,否则此类将不起作用。此外,如果合并的游标在“_id”列中具有重叠值,则使用MergeCursor与此类不起作用。
这是你得到的错误。
此外,如果搜索过,您可以在已经回答的SO问题上找到解决方案,例如one.