public class DBCreation extends SQLiteOpenHelper
{
Context context;
public DBCreation(Context context)
{
super(context, "LBRDatabase", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) throws SQLException
{
db.execSQL("create table Reminders(id integer primary key autoincrement,description text,address text not null,latitude double not null,longitude double not null,radius text,reminderdate text,remindertime text);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
try
{
db.execSQL("drop table if exists Reminders ");
onCreate(db);
}
catch (Exception e)
{
Toast.makeText(context, ""+e, Toast.LENGTH_LONG).show();
}
}
}
此代码有什么问题? 它给了我以下错误?
06-18 08:54:30.843: E/Database(6452): Error inserting Reminders
06-18 08:54:30.843: E/Database(6452): android.database.sqlite.SQLiteException: no such table: Reminders: , while compiling: INSERT INTO Reminders(radius, longitude, latitude, reminderdate, address, description, remindertime) VALUES(?, ?, ?, ?, ?, ?, ?);
表格正在创建。插入的顺序也不合适。首先是半径然后是经度,然后经度不是我创建表格的顺序。
public class DBOperations
{
Context context;
SQLiteDatabase db;
DBCreation createdb;
public DBOperations(Context context)
{
this.context = context;
createdb = new DBCreation(context);
}
public DBCreation OpenDB() throws SQLException
{
db = createdb.getWritableDatabase();
return createdb;
}
public void CloseDB()
{
db.close();
}
public long addReminder(String description,String address,double latitude,double longitude,String radius,String date,String time)
{
ContentValues cv = new ContentValues();
cv.put("description", description);
cv.put("address", address);
cv.put("latitude", latitude);
cv.put("longitude", longitude);
cv.put("radius", radius);
cv.put("reminderdate", date);
cv.put("remindertime", time);
return db.insert("Reminders", null, cv);
}
public long deleteReminder(int id)
{
return db.delete("Reminders", "_ID="+id, null);
}
public long updateReminder(int id,String desc,String addr,double lat,double lon,int radius,String date,String time)
{
ContentValues cv = new ContentValues();
cv.put("description", desc);
cv.put("address", addr);
cv.put("latitude", lat);
cv.put("longitude", lon);
cv.put("radius", radius);
cv.put("reminderdate", date);
cv.put("remindertime", time);
return db.update("Reminders", cv, "_ID="+id, null);
}
public Cursor showReminders()
{
Cursor c = db.query("Reminders", null, null , null, null, null, null);
if(c!=null)
{
return c;
}
else
{
return null;
}
}
}
答案 0 :(得分:1)
您的CREATE TABLE语法不正确。来自fine manual:
AUTOINCREMENT关键字
如果列的类型为
INTEGER PRIMARY KEY AUTOINCREMENT
,则会使用稍微不同的ROWID
选择算法。
请注意,它表示 AUTOINCREMENT ,而不是 AUTO_INCREMENT 。因此,您的Reminders
表确实没有被创建,INSERT正在抱怨。
答案 1 :(得分:1)
答案 2 :(得分:0)
创建表时,对于主键“_id”,假设它是“AUTOINCREMENT”而中间没有下划线。你可以尝试一下吗?
答案 3 :(得分:0)
在你的添加提醒方法中,你将值放入cv的地方,你试过为字段_id设置一个虚拟值吗?我知道您已将列设置为自动增量,但它也是不能为null的主键。 为该列设置虚拟值可能会有所帮助。
答案 4 :(得分:0)
我遇到了同样的问题,但我的查询结构不正确。这是我所做的查询的正确样本。希望这有助于你:)
CREATE TABLE "Configuration" (
"_id" INTEGER PRIMARY KEY AUTOINCREMENT,
"id_Users" INTEGER,
"Key" VARCHAR(255),
"Value" VARCHAR(255)
);
答案 5 :(得分:0)
首先创建表然后打开表追加或打开写入模式然后插入数据
答案 6 :(得分:0)
for newbee ....创建简单的databaseHelper类,如下所示(为什么使用两个不同的类&创建混淆):
public class DBController extends SQLiteOpenHelper {
private static final String LOGCAT = null;
public DBController(Context applicationcontext) {
super(applicationcontext, "androidsqlite.db", null, 1);
Log.d(LOGCAT,"Created");
}
@Override
public void onCreate(SQLiteDatabase database) {
String query;
query = "CREATE TABLE animals ( animalId INTEGER PRIMARY KEY, animalName TEXT)";
database.execSQL(query);
Log.d(LOGCAT,"animals Created");
}
@Override
public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
String query;
query = "DROP TABLE IF EXISTS animals";
database.execSQL(query);
onCreate(database);
}
public void insertAnimal(HashMap<String, String> queryValues) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("animalName", queryValues.get("animalName"));
database.insert("animals", null, values);
database.close();
}
public int updateAnimal(HashMap<String, String> queryValues) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("animalName", queryValues.get("animalName"));
return database.update("animals", values, "animalId" + " = ?", new String[] { queryValues.get("animalId") });
}
public void deleteAnimal(String id) {
Log.d(LOGCAT,"delete");
SQLiteDatabase database = this.getWritableDatabase();
String deleteQuery = "DELETE FROM animals where animalId='"+ id +"'";
Log.d("query",deleteQuery);
database.execSQL(deleteQuery);
}
public ArrayList<HashMap<String, String>> getAllAnimals() {
ArrayList<HashMap<String, String>> wordList;
wordList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM animals";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("animalId", cursor.getString(0));
map.put("animalName", cursor.getString(1));
wordList.add(map);
} while (cursor.moveToNext());
}
return wordList;
}
public HashMap<String, String> getAnimalInfo(String id) {
HashMap<String, String> wordList = new HashMap<String, String>();
SQLiteDatabase database = this.getReadableDatabase();
String selectQuery = "SELECT * FROM animals where animalId='"+id+"'";
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
wordList.put("animalName", cursor.getString(1));
} while (cursor.moveToNext());
}
return wordList;
}
}
用于插入数据:
DBController controller = new DBController(myactivity.this);
HashMap<String, String> queryValues = new HashMap<String, String>();
queryValues.put("animalName", animalName.getText().toString());
controller.insertAnimal(queryValues);