私はウェブ上のチュートリアルを通じてAndroidデータベースで手を試していましたが、そのような列の例外を取得していません.Hereは私のコードです:
public class MySQLiteHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "db_name.db";
private static final int DB_VERSION = 1;
public static final String DB_TABLENAME="vaib";
//columns
public static final String DB_COL1 = "_id";
public static final String DB_COL2 = "comments";
private static final String DB_CREATE = " create table "
+ DB_TABLENAME + "(" + DB_COL1 +
" integer primary key autoincrement, " + DB_COL2
+ " text );";
public MySQLiteHelper(Context context)
{
super(context,DB_NAME,null,DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DB_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS" + DB_TABLENAME);
onCreate(db);
}
}
//comment.java
//this class will store data in database & display value on UI.
public class Comment {
private long id;
private String comments;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
// Will be used by the ArrayAdapter in the ListView
@Override
public String toString() {
return comments;
}
}
//main activity
public class MainActivity extends ListActivity {
private CommentsDataSource datasource;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datasource = new CommentsDataSource(this);
datasource.open();
List<Comment> values = datasource.getAllComments();
// Use the SimpleCursorAdapter to show the
// elements in a ListView
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
// Will be called via the onClick attribute
// of the buttons in main.xml
public void onClick(View view) {
@SuppressWarnings("unchecked")
ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
Comment comment = null;
switch (view.getId()) {
case R.id.add:
String[] comments = new String[] { "Cool", "Very nice", "Hate it" };
int nextInt = new Random().nextInt(3);
// Save the new comment to the database
comment = datasource.createComment(comments[nextInt]);
adapter.add(comment);
break;
case R.id.delete:
if (getListAdapter().getCount() > 0) {
comment = (Comment) getListAdapter().getItem(0);
datasource.deleteComment(comment);
adapter.remove(comment);
}
break;
}
adapter.notifyDataSetChanged();
}
@Override
protected void onResume() {
datasource.open();
super.onResume();
}
@Override
protected void onPause() {
datasource.close();
super.onPause();
}
}
ログメッセージは次のとおりです。
09-30 17:21:42.679: E/AndroidRuntime(30819): FATAL EXCEPTION: main
09-30 17:21:42.679: E/AndroidRuntime(30819): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.databaseapp/com.example.databaseapp.MainActivity}: android.database.sqlite.SQLiteException: no such column: comments: , while compiling: SELECT _id, commentss FROM vaib
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.os.Looper.loop(Looper.java:130)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.app.ActivityThread.main(ActivityThread.java:3687)
09-30 17:21:42.679: E/AndroidRuntime(30819): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 17:21:42.679: E/AndroidRuntime(30819): at java.lang.reflect.Method.invoke(Method.java:507)
09-30 17:21:42.679: E/AndroidRuntime(30819): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-30 17:21:42.679: E/AndroidRuntime(30819): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-30 17:21:42.679: E/AndroidRuntime(30819): at dalvik.system.NativeStart.main(Native Method)
09-30 17:21:42.679: E/AndroidRuntime(30819): Caused by: android.database.sqlite.SQLiteException: no such column: commentss: , while compiling: SELECT _id, commentss FROM vaib
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1357)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1236)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1190)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1272)
09-30 17:21:42.679: E/AndroidRuntime(30819): at dao.CommentsDataSource.getAllComments(CommentsDataSource.java:56)
09-30 17:21:42.679: E/AndroidRuntime(30819): at com.example.databaseapp.MainActivity.onCreate(MainActivity.java:23)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)