一旦将数据插入sqlite,就会出现错误

时间:2013-09-13 03:48:11

标签: java android sqlite

一旦我打开我的应用程序,它就会提示我很遗憾,数据库已停止

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);
  }
}

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

2 个答案:

答案 0 :(得分:1)

修改onCreate(SQLiteDatabase数据库){},如下所示

@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)" );

}

答案 1 :(得分:0)

你应该像这样创建一个表。

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