Reputation: 832
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();
}
}
}
What is the problem with this code? It is giving me the following error?
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(?, ?, ?, ?, ?, ?, ?);
The table is getting created.The order of insertion is also not proper.It is first taking radius then longitude, then longitude which is not the order in which i created the table.
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;
}
}
}
Upvotes: 0
Views: 441
Reputation: 18978
for newbee.... create simple databaseHelper class like below(Why use two different class & create confusion):
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;
}
}
for insert data:
DBController controller = new DBController(myactivity.this);
HashMap<String, String> queryValues = new HashMap<String, String>();
queryValues.put("animalName", animalName.getText().toString());
controller.insertAnimal(queryValues);
Upvotes: 0
Reputation: 321
first create table then open table in append or open to write mode and then insert data
Upvotes: 0
Reputation: 3672
I faced the same problem, but my query structure was incorrect. Here the correct sample of a query that I made. Hope this help ya :)
CREATE TABLE "Configuration" (
"_id" INTEGER PRIMARY KEY AUTOINCREMENT,
"id_Users" INTEGER,
"Key" VARCHAR(255),
"Value" VARCHAR(255)
);
Upvotes: 0
Reputation: 389
In your add reminder method, the place where u are putting values into cv, have you tried putting a dummy value for the field _id? I understand that you have set the column to autoincrement but it is also the primary key which cannot be null. Putting a dummy value for that column might help.
Upvotes: 0
Reputation: 5721
I think your database is not created
You have to create database proper way with format
here you can findout example
.
If you have any query then put comment.
Upvotes: 1
Reputation: 156
when creating the table, for the primary key "_id", it is suppose to be "AUTOINCREMENT" without the underscore in between. Probably you can try that?
Upvotes: 0
Reputation: 434635
Your CREATE TABLE syntax is incorrect. From the fine manual:
The AUTOINCREMENT Keyword
If a column has the type
INTEGER PRIMARY KEY AUTOINCREMENT
then a slightly differentROWID
selection algorithm is used.
Note that it says AUTOINCREMENT, not AUTO_INCREMENT. So your Reminders
table really isn't being created and the INSERT is rightfully complaining.
Upvotes: 1