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

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

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

最佳答案

修改 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/

相关文章:

java - Spring boot + JPA + Hibernate + Oracle 在应用程序启动期间不会自动创建表,并且日志中没有报告错误

android - 使用 gcc 的 C++ 前端编译 C 代码的好处

android - 在 Android 中隐藏 RadioButton

Java TLS-PSK 套接字

sqlite - 我如何知道 db 文件的 sqlite 版本和密码?

.net - 使用 .NET 应用程序部署 SQLite 时的 list

java - 我的 Java 代码有什么问题,因为它一直使用默认答案进行回答?

java - 如何创建所有位都为 1 的 long 值

java - 在没有集合的ArrayList中查找最小整数

csv - 如何在 sqlite 中使用 CR(回车)而不是 LF(换行)输出?