我正在尝试通过android 3.1创建SQLite数据库。但是它不能作为下面的代码工作。请帮助谢谢。
主要活动.class
public class MainActivity extends AppCompatActivity { DatabaseHelper myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this); // this is contact, this is new extension of DatabaseHelper
}
}
DatabaseHelper.class
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Iters.db";
public static final String TABLE_NAME = "iters_table"; //called onCreate method
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "SURNAME";
public static final String COL_4 = "MARKS";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
SQLiteDatabase db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE TABLE_NAME (ID INTEGER PRIMARY KEY AUTOINCREMENT," +
"NAME TEXT," +
"SURNAME TEXT," +
"MARKS INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
onCreate(db);
}
}
答案 0 :(得分:0)
似乎您的问题可能是您在命名表 TABLE_NAME 而不是 iters_table 。
您可以使用:-
对此进行更正。db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY," +
"NAME TEXT," +
"SURNAME TEXT," +
"MARKS INTEGER)");