我遇到了一个运行时错误,“不幸的是,MyOwnSQLite已经停止”,同时从http://android-er.blogspot.in/2011/06/simple-example-using-androids-sqlite_02.html
执行本教程logcat的:
- 错误打开跟踪文件:没有这样的文件或目录(2)
- (1)没有这样的表:Zomibedatabase
- 插入名称= Jenny -android.database.sqlite.SQLiteException:没有这样的表:Zombiedatabase(代码1):,同时编译:INSERT INTO Zombiedatabase(Name)VALUES(?)
Adapter.java
package com.example.myownsqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class Adapter{
public static final String DATABASE_NAME = "Zombiedatabase";
public static final String DATA_TABLE_NAME = "Mytable";
public static final int VERSION = 1;
public static final String EMPLOYEE_ID = "_id";
public static final String EMPLOYEE_NAME = "Name";
private static final String SCRIPT_CREATE_DB = "create table " + DATA_TABLE_NAME +" ("
+ EMPLOYEE_ID + " integer primary key autoincrement, "
+ EMPLOYEE_NAME + " text not null);";
// + EMPLOYEE_PAY + " text not null"
// + EMPLOYEE_POST + "text not null);";
private SQLiteHelper sqlitehelp;
private SQLiteDatabase db;
private Context context;
public Adapter(Context c){
context = c;
}
//Methods
//I. read and write
public Adapter openToRead() throws android.database.SQLException {
sqlitehelp = new SQLiteHelper (context,DATABASE_NAME,null,VERSION);
db = sqlitehelp.getReadableDatabase();
return this;
}
public Adapter openToWrite() throws android.database.SQLException{
sqlitehelp = new SQLiteHelper (context,DATABASE_NAME,null,VERSION);
db = sqlitehelp.getWritableDatabase();
return this;
}
//II. Close
public void close(){
sqlitehelp.close();
}
//III. Inserting Data
public long insert(String content1){
ContentValues contentValues = new ContentValues();
contentValues.put(EMPLOYEE_NAME,content1);
return db.insert(DATABASE_NAME,null,contentValues);
}
//IV. Delete all
public int deleteall(){
return db.delete(DATABASE_NAME,null,null);
}
//V.Queue all
public Cursor queueall(){
String[] columns = new String[] {EMPLOYEE_ID,EMPLOYEE_NAME};
Cursor cursor = db.query(DATABASE_NAME, columns, null,null,null,null,null,null);
//http://developer.android.com/reference/android/database/sqlite/SQLiteQueryBuilder.html
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_DB);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
}
主要活动
package com.example.myownsqlite;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends Activity {
private Adapter avatar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listemployee = (ListView) findViewById(R.id.contentlist);
avatar = new Adapter(this);
avatar.openToWrite();
avatar.insert("Jenny");
avatar.insert("Jane");
avatar.insert("Jess");
Cursor cursor = avatar.queueall();
startManagingCursor(cursor);
String[] from = new String[]{avatar.EMPLOYEE_NAME};
int[] to = new int[]{R.id.text};
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,R.layout.row,cursor,from,to);
listemployee.setAdapter(cursorAdapter);
avatar.close();
}
}
答案 0 :(得分:1)
插入时必须指定表名而不是数据库名,与其他方法(crud)相同:
public long insert(String content1){
ContentValues contentValues = new ContentValues();
contentValues.put(EMPLOYEE_NAME,content1);
return db.insert(DATA_TABLE_NAME,null,contentValues);
}
答案 1 :(得分:0)
感谢alberto,补充一下,另一个错误是 数据库和字符串名称必须为大写。
public static final String DATABASE_NAME = "ZOMBIEDATA";
public static final String DATA_TABLE_NAME = "TABLE";