我正在尝试为我的Android应用程序创建一个SQLite数据库。我有所有的代码,但我在logcat中得到一个错误,说没有这样的表。我想我有正确的代码,但如果你能看看我是否遗漏了什么,我将不胜感激。
package com.example.rory.dbtest;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
public static final String KEY_ROWID = "id";
public static final String KEY_ITEM = "item";
public static final String KEY_LITRES = "litres";
//public static final String KEY_COURSE = "course";
//public static final String KEY_NOTES = "notes";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "DripDrop";
private static final String DATABASE_TABLE = "table1";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table if not exists assignments (id integer primary key autoincrement, "
+ "item VARCHAR not null, litres date );";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a record into the database---
public long insertRecord(String item, String litres)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ITEM, item);
initialValues.put(KEY_LITRES, litres);
//initialValues.put(KEY_COURSE, course);
//initialValues.put(KEY_NOTES, notes);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---deletes a particular record---
public boolean deleteContact(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
//---retrieves all the records---
public Cursor getAllRecords()
{
return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_ITEM,
KEY_LITRES}, null, null, null, null, null);
}
//---retrieves a particular record---
public Cursor getRecord(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_ITEM, KEY_LITRES},
KEY_ROWID + "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---updates a record---
public boolean updateRecord(long rowId, String item, String litres)
{
ContentValues args = new ContentValues();
args.put(KEY_ITEM, item);
args.put(KEY_LITRES, litres);
//args.put(KEY_COURSE, course);
//args.put(KEY_NOTES, notes);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
应用程序崩溃后的logcat错误是:(抱歉格式化我无法完全正确)。
package com.pinchtapzoom;
Caused by: android.database.sqlite.SQLiteException: no such table: table1 (code 1): , while compiling: SELECT id, item, litres FROM table1
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
答案 0 :(得分:1)
您似乎想要将表名创建为assignments
并访问table1
中的数据。所以更改
private static final String DATABASE_TABLE = "table1";
到
private static final String DATABASE_TABLE = "assignments";
答案 1 :(得分:0)
看一下你桌子上的名字:
private static final String DATABASE_TABLE = "table1";
您的查询:
"create table if not exists assignments bla bla"
它们不相同,这就是为什么会出现此错误。
您需要更改其中一个,以便名称相同。