エラーは、アプリケーションが予期せず停止し、queueall メソッドでエラーが表示されますCursor
。プログラムを実行しているときに、アプリケーションが予期せず停止したというエラーがあります。データベースを作成するための設定はありますか?
public class SQLiteAdapter {
public static final String MYDATABASE_NAME = "MY_DATABASE";
public static final String MYDATABASE_TABLE = "MY_TABLE";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_CONTENT = "Content";
// Create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE = "create table "
+ MYDATABASE_TABLE + "(" + KEY_CONTENT + "text ot null);";
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public SQLiteAdapter(Context c) {
// TODO Auto-generated constructor stub
context = c;
}
// Write to the database
public SQLiteAdapter openToWrite() throws android.database.SQLException
{
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
// TODO Auto-generated method stub
}
// Read from database
public SQLiteAdapter openToRead() throws android.database.SQLException
{
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public void close()
{
sqLiteHelper.close();
}
public long insert(String content)
{
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_CONTENT, content);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}
//Delete data from database
public int deleteAll() {
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}
public String queueAll()
{
String[] columns = new String[]{KEY_CONTENT};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int index_CONTENT = cursor.getColumnIndex(KEY_CONTENT);
// moving cursor from 1st to next
for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext())
{
result = result + cursor.getString(index_CONTENT)+ "\n";
}
return result;
}
public class SQLiteHelper extends SQLiteOpenHelper
{
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
arg0.execSQL(SCRIPT_CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
}
ログキャットは:
02-23 23:30:29.819: E/AndroidRuntime(362): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
02-23 23:30:29.819: E/AndroidRuntime(362): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
02-23 23:30:29.819: E/AndroidRuntime(362): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
02-23 23:30:29.819: E/AndroidRuntime(362): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229)
02-23 23:30:29.819: E/AndroidRuntime(362): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
02-23 23:30:29.819: E/AndroidRuntime(362): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
02-23 23:30:29.819: E/AndroidRuntime(362): at com.example.androidsqlite.SQLiteAdapter.queueAll(SQLiteAdapter.java:80)
02-23 23:30:29.819: E/AndroidRuntime(362): at com.example.androidsqlite.AndroidSQLite.onCreate(AndroidSQLite.java:35)
02-23 23:30:29.819: E/AndroidRuntime(362): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-23 23:30:29.819: E/AndroidRuntime(362): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-23 23:30:29.819: E/AndroidRuntime(362): ... 11 more