Android SQLite: insert() issues - android

i'm trying to insert some data into a SQLite3 Database on an Android system.
This method should takes three strings and insert them into a database:
public void createEntry(String description, String reps, String weight)
{
ContentValues cv = new ContentValues();
cv.put(KEY_DESCRIPTION, description);
cv.put(KEY_REPS, reps);
cv.put(KEY_WEIGHT, weight);
sqlDB.insert(DATABASE_TABLE, null, cv);
}
The columns(KEY_DESCRIPTION,..) are all defined as TEXT NOT NULL and I also added an auto incrementing column.
In my main activity I'm using this code to get input from EditText Widgets
String description = sqlDescription.getText().toString();
then I'm creating an instance of my DB class:
DB entry = new DB(MainActivity.this);
entry.open();
entry.createEntry(description, reps, weight);
entry.close();
open() uses the SQLiteOpenHelper and sets up database:
public DB open()
{
sqlHelper = new dbHelper(sqlContext);
sqlDB = sqlHelper.getWritableDatabase();
return this;
}
But when I call this method:
entry.createEntry(description, reps, weight);
I get following error:
(1) near "Table": syntax error
E/SQLiteDatabase(19344): Error inserting Description=test Weight=tesz Reps=test
E/SQLiteDatabase(19344): android.database.sqlite.SQLiteException: near "Table": syntax error
(code 1): , while compiling: INSERT INTO Table(Description,Weight,Reps) VALUES (?,?,?)

TABLE is a reserved word in SQL; you should not use it as the name of the table.
Try changing it to:
private static final String DATABASE_TABLE = "myTable";

Related

Insert data into SQLite table in the same row

I am trying to insert data in an SQLite database from different places in different columns at a time. When I view the database, each insert takes one row. My databases lookes like this:
How do I achieve this? Any suggestions?
In order to insert a new row within your database, you need to add the following code into the Java class that manages your SQLite database:
public long insertRow(String name, String email)
{
ContentValues contentValues = new ContentValues();
contentValues.put(rowName, name);
contentValues.put(rowEmail, email);
return database.insert(databaseTable, null, contentValues);
}
The variable database refers to the database you attempting to insert the row. The variable databaseTable refers to the table you want to insert the row into. If you are attempting to update an existing column, you need to add the following code into the Java class that manages your database:
public boolean updateRow(long id, String name, String email)
{
ContentValues contentValues = new ContentValues();
contentValues.put(rowName, name);
contentValues.put(rowEmail, email);
return database.update(databaseTable, contentValues, rowId + “=” + id, null) > 0;
}
In order to call this function, you need to pass the id of the row and the desired values to pass in order to update them.

Insert into sqlite with default value

I'm working on a android program with SQLite. I'm trying to create a datebase with two tables related by a foreign key, and I want to automaticaly populate one entry of the mother table using the insert funcion. But this generate an SQLite error.
Here is the funcion to insert an entry into the mother class
private long new_event(SQLiteDatabase db)
{
ContentValues values = new ContentValues();
long id = db.insert("EVENT",null,values);
return id;
}
Here is the function to insert an entry into the child class
public long new_specific_event(SQLiteDatabase db)
{
long id_event = new_event(db);
ContentValues values = new ContentValues();
values.put("id_event", id_event);
values.put("whatsoever", "whatsoever");
long id = db.insert("SPECIFIC_EVENT",null,values);
return id;
}
Here is the mother table
CREATE TABLE EVENT (id INTEGER PRIMARY KEY AUTOINCREMENT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
and here is the child table
CREATE TABLE SPECIFIC_EVENT (id INTEGER PRIMARY KEY AUTOINCREMENT, id_event NUMBER,whatsoever TEXT,FOREIGN KEY(id_event) REFERENCES EVENT(id));
This result into the following error
android.database.sqlite.SQLiteException: near "null": syntax error (code 1): , while compiling: INSERT INTO EVENT(null) VALUES (NULL)
I could do it using this and the db.execSQL() funcion, but then I have no access to the id of the entry I just create.
So, how can I use the insert funcion to insert an entry with just default value?
With ContentValues you need to put at least one value. To get a default value, put a null for a value for a column:
ContentValues values = new ContentValues();
values.putNull("_id");
long id = db.insert("EVENT",null,values);
Inserting a completely empty row is not possible, so the insert() method has the parameter nullColumnHack to allow you to specify a column that gets a NULL value in this case:
ContentValues values = new ContentValues();
long id = db.insert("EVENT", "_id", values);

Android unable to escaping single quote character while SQLite database update

Hi in my database i have a string like "computer's" , i am fetching it from database and displaying listview, when i click on the list item it should store to one of my database table. I am doing it as below but it is getting crashed
public void updateFav(String key, int value) {
SQLiteDatabase db = mDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
C_FAV=C_FAV.replaceAll("'","''");
values.put(C_FAV, value);
db.update(tableName, values, C_KEY + "='" + key.trim() + "'", null);
db.close();
}
Below is my trace
12-12 17:55:49.291: E/AndroidRuntime(18184): android.database.sqlite.SQLiteException: near "s":
syntax error (code 1): , while compiling: UPDATE fav SET favorite=? WHERE key='computer's'
You're escaping the apostrophes in C_FAV which seems to be a column name but not in key.
Consider using ? placeholder and bind arguments instead, e.g.
db.update(tableName, values, C_KEY + "=?", new String[] { key.trim() });

Insert in SQLiteOpenHelper dosen't work correctly

I have a problem with SQLite in android , I tried a simple insert in a table but I have an exception and in the exception I read this :
INSERT INTO Biblio(Auteur,Image,Livre) VALUES (?,?,?)
this is my method :
public void AddBook(Bibliothèque bibliothèque)
{
SQLiteDatabase db=this.getWritableDatabase();
//this.open();
ContentValues values=new ContentValues();
values.put(KEY_Auteur,bibliothèque.getNomAuteur());
values.put(KEY_Livre,bibliothèque.getNomLivre());
values.put(Key_photo,bibliothèque.geturiimage().toString());
db.insert("Biblio",null,values);
db.close();
}
and this is my variables:
private static final String TABLE_Nom = "Biblio";
private static final String KEY_ID = "id";
private static final String KEY_Auteur = "Auteur";
private static final String KEY_Livre = "Livre";
private static final String Key_photo="Image";
the problem I don't know why but db.insert invert beetween Image and Livre and because of that I have an exception and I don't know why the value that I insert are ? ? ? in debug I can see that the value are correct ! . normaly the true expression must be :
INSERT INTO Biblio(Auteur,Livre,Image) VALUES (cc,test,(uri of picture))
this is the entire exception :
2927-2927/com.example.myapplication4.app E/SQLiteLog﹕ (1) table Biblio has no column named Livre
05-15 02:53:54.436 2927-2927/com.example.myapplication4.app E/SQLiteDatabase﹕ Error inserting Auteur=gsopfkgop Image=content://media/external/images/media/10 Livre=fhoksgkp
android.database.sqlite.SQLiteException: table Biblio has no column named Livre (code 1): , while compiling: INSERT INTO Biblio(Auteur,Image,Livre) VALUES (?,?,?)
USE This Query to Create Table.
db.execSQL
("CREATE TABLE Biblio (id INTEGER PRIMARY KEY AUTOINCREMENT ,Auteur TEXT,Livre TEXT,Image TEXT)");
then You have to insert record using Below code:
public void AddBook(Bibliothèque bibliothèque)
{
SQLiteDatabase db=this.getWritableDatabase();
//this.open();
ContentValues values=new ContentValues();
values.put(Auteur,bibliothèque.getNomAuteur());
values.put(Livre,bibliothèque.getNomLivre());
values.put(Image,bibliothèque.geturiimage().toString());
db.insert("Biblio",null,values);
db.close();
}
I hope its useful to you..
2927-2927/com.example.myapplication4.app E/SQLiteLog﹕ (1) table Biblio has no column named Livre
Your table doesn't have a column named Livre, either you have a spelling mistake here, in your table creation code, or you haven't defined a column named Livre.
You should try to reinstall your app?
Also, if your db schema has been changed after your app installed in your test device, you have to increase db version parameter to invoke onUpdate() method or reinstall your app to invoke onCreate().

android.database.sqlite.SQLiteException: unrecognized token:

I try to update database table and this is my code:
public void updatefiletable(String filename, String v1, String v2){
AndroidOpenDbHelper androidOpenDbHelperObj = new AndroidOpenDbHelper(this);
SQLiteDatabase sqliteDatabase = androidOpenDbHelperObj.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(AndroidOpenDbHelper.COLLUMN_NAME_FILE_CLOUD, v1);
values.put(AndroidOpenDbHelper.COLLUMN_NAME_FILE_DATE_UPLOADING, v2);
sqliteDatabase.update(AndroidOpenDbHelper.TABLE_FILE, values, AndroidOpenDbHelper.COLUMN_NAME_FILE_NAME+"="+filename, null);
sqliteDatabase.close();
}
when I call my method with file_name values egal priv_priv_secondfile_2012-06-15.pdf I get that in the logcat:
android.database.sqlite.SQLiteException: unrecognized token: "15.pdf": ,
while compiling: UPDATE file_table SET file_cloud_column=?,
file_date_upload_column=?
WHERE file_name_column=priv_priv_secondfile_2012-06-15.pdf
how can I fix it?
You need to escape the filename parameter. The punctuation in the filename is confusing SQLite. You could do it by surrounding the filename in 'single quotes' in the string you pass in to SQLite, but it's cleaner and safer to pass it as a separate argument, like this:
sqliteDatabase.update(AndroidOpenDbHelper.TABLE_FILE, values,
AndroidOpenDbHelper.COLUMN_NAME_FILE_NAME+"=?", new String[] {filename});

Categories

Resources