gpt4 book ai didi

java - 向 sqlite 插入数据后不断出现错误

转载 作者:行者123 更新时间:2023-12-01 14:07:36 24 4
gpt4 key购买 nike

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

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

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

最佳答案

修改 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)" );

}

关于java - 向 sqlite 插入数据后不断出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18777972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com