2012-09-30 51 views
0

我是通过网络上的教程想我在android系统数据库的手,但没有得到这样的列例外,请帮助。这里是我的代码:获取没有这样的列异常?

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) 
+0

请提供'CommentsDataSource'的代码。如果它是您使用的框架或库,请告诉我们它是什么。 – kdehairy

回答

0

我猜某处你拼写错误 - > commentss,因为这是错误。再次检查您的代码,并确保在重新启动之前确实删除了数据库。只要改变

private static final String DB_NAME = "db_name.db"; 

private static final String DB_NAME = "db_name01.db"; 

等,以避免混乱 - 并保持增加01〜02等,直到你的数据库就可以了。

在更改数据库时留下相同的名称可能会导致出现奇怪的错误。

看你的代码,我不能在任何地方找到commentss - 所以一定在别处或者从一些你之前尝试的...

+0

其实早些时候我曾在eclipse中写过列名作为评论,但是在论坛中发布时删除了一个来自commentss但忘记从日志消息中删除的问题是一些我认为不是拼写错误的地方。请为这个错误运行 – vaibvorld

+0

不,那个是在日志清楚的错误---引起:android.database.sqlite.SQLiteException:没有这样的列:commentss:,而编译:SELECT _id,commentss FROM vaib。这是你的问题,或者你有另一个错误。 – Simon

1

仔细阅读除外:

SQLiteException: no such column: commentss: , while compiling: SELECT _id, commentss FROM vaib 

这告诉我们,有没有列名为commentss。您的专栏名称为comments,而不是commentss ...只需修复您的SELECT-声明中的错字。

0

这一切都基于猜测和类似的问题。 您正在使用库或框架来代表您使用反射来实现数据访问层。

那就是:
它在你的模型类使用的成员变量的名字(例如Comment类)与数据库中的类似领域访问表comments

所以..基于这个假设,错误是在下面一行:

private String comments; 
Comment


它应该是:

private String comment; 

而不在该行的末尾的(一个或多个)。

让我知道它是否工作。

0

的错误指示错误可能是使用方法getAllComments()从文件 “CommentsDataSource.java”,行56 MainActivity.java

List<Comment> values = datasource.getAllComments(); 

打电话时:

“在道.CommentsDataSource.getAllComments(CommentsDataSource.java:56)“

请检查此行,确保在此行中的查询中重复使用了相同的变量名DB_COL2。