0
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;
    }
}
}
4

7 回答 7

1

您的 CREATE TABLE 语法不正确。来自精美手册

AUTOINCREMENT 关键字

如果列具有类型INTEGER PRIMARY KEY AUTOINCREMENT,则使用稍微不同的ROWID选择算法。

请注意,它说的是AUTOINCREMENT,而不是AUTO_INCREMENT。因此,您的Reminders表确实没有被创建,并且 INSERT 有理有据地抱怨。

于 2013-06-18T03:47:14.617 回答
1

我想你的database is not created

你必须以正确的方式创建数据库,格式 在这里你可以找到example

如果您有任何疑问,请发表评论。

于 2013-06-18T04:22:19.507 回答
0

在您的添加提醒方法中,您将值放入 cv 的地方,您是否尝试过为字段 _id 放置一个虚拟值?我了解您已将该列设置为自动增量,但它也是不能为空的主键。为该列设置一个虚拟值可能会有所帮助。

于 2013-06-18T04:40:56.397 回答
0

创建表时,对于主键“_id”,假定为“AUTOINCREMENT”,中间没有下划线。大概可以试试吧?

于 2013-06-18T03:47:59.353 回答
0

我遇到了同样的问题,但是我的查询结构不正确。这是我提出的查询的正确示例。希望这对你有帮助:)

   CREATE TABLE "Configuration" (
"_id" INTEGER PRIMARY KEY AUTOINCREMENT, 
"id_Users" INTEGER, 
"Key" VARCHAR(255), 
"Value" VARCHAR(255)
    );
于 2013-06-18T04:43:32.780 回答
0

对于 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);

这是示例示例

于 2013-06-20T05:02:03.100 回答
0

首先创建表然后以追加方式打开表或以写入模式打开然后插入数据

于 2013-06-18T05:07:10.500 回答