0

一旦我打开我的应用程序,它就会提示我不幸的是,数据库已停止

public class Databasetest extends Activity {

private CommentDataSource datasource;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_databasetest);


    datasource = new CommentDataSource(this);
    datasource.open();
    }
}

以下是我得到的错误,有人可以告诉我这是什么意思吗?..

(1) near "studentinfo_db": syntax error

这是我的 DataCommentSource :

 public class CommentDataSource {


  private SQLiteDatabase db;
  private MySQLiteHelper dbHlp;
  private String[] allColumns = { StudentInfo.COL_ROW_ID,
      StudentInfo.COL_STUDENT_NAME };

  public CommentDataSource(Context context) {
dbHlp = new MySQLiteHelper(context);
  }

  public void open() throws SQLException {
db= dbHlp.getWritableDatabase();
  }

  public void close() {
dbHlp.close();
  }
}

这是我的 MySQLiteHelper:

public class MySQLiteHelper extends SQLiteOpenHelper {


private static final String DATABASE_NAME = "studentsresult.db";
private static final int DATABASE_VERSION = 1;


public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase database) {
    database.execSQL(StudentInfo.SQL_TABLE_NAME);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(MySQLiteHelper.class.getName(),
        "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + StudentInfo.SQL_TABLE_NAME);
    onCreate(db);
  }
}

上面我已经上传了我的源代码。

4

2 回答 2

1

修改 onCreate(SQLiteDatabase database){} 如下

@Override
public void onCreate(SQLiteDatabase database) {

database.execSQL("CREATE TABLE "+ StudentInfo.SQL_TABLE_NAME + "(" + 
                        StudentInfo.COL_ROW_ID + " INT PRIMARY KEY," +
                        StudentInfo.COL_STUDENT_NAME+ " TEXT)" );

}
于 2016-02-05T06:00:06.027 回答
0

您应该像这样在创建时创建一个表。

@Override
public void onCreate(SQLiteDatabase database) {
    database.execSQL("CREATE TABLE "+StudentInfo.SQL_TABLE_NAME);
}
于 2013-09-13T04:27:33.040 回答