我已经在SQLite android中创建了两个表,但每当我运行此代码时,它表示没有找到具有该名称的表,我已经在许多设备上尝试过但我仍然无法在以下代码中找到错误。
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table peoples" +
"(personid integer primary key autoincrement," +
" pname text," +
" pvehicle text," +
" ptime text," +
" pdate text," +
" startpoint text," +
" endpoint text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS peoples");
}
public boolean insertPeoples(String pname,String pvehicle, String ptime, String pdate, String start, String end)
{
try {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("pname",pname);
values.put("pvehicle",pvehicle);
values.put("ptime",ptime);
values.put("pdate",pdate);
values.put("startpoint",start);
values.put("endpoint",end);
db.insert("peoples",null,values);
}
catch (SQLiteException ex)
{
Log.i("MyLog",ex.getMessage());
}
return true;
}
public Cursor getAllpeoples()
{
Cursor res = null;
try {
SQLiteDatabase database = this.getReadableDatabase();
res = database.rawQuery("select * from peoples",null);
}
catch (SQLiteException ex)
{
Log.e("MyLog",ex.getMessage());
}
return res;
}
public void deletePeoples()
{
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("delete from peoples");
}
还有另一个表具有相似的代码并且工作正常但是这个第二个表由另一个类处理并且工作正常但是这段代码显示错误,没有找到名称people的表,所以它返回空指针异常而我尝试获取记录。
答案 0 :(得分:-1)
我认为你在编写create table代码时出错了。看看这个
public static String createTable="create table details(_id integer Primary key autoincrement,firstname text,lastname text,email text,contactno text)";
public void onCreate(SQLiteDatabase db)
{
db.execSQL(createTable);
}
主键中的字母“P”是大写。