Sqlite first use does not save record - android

I have a problem with my sqlite.
when i install the app and database is empty. the first entry is never stored.
there is any way to solve this???
this is the code to insert data :
final ContentValues values=new ContentValues();
values.put("cod_comp",pub_cod_comp);
values.put("telefone_comp", pub_telf_comp);
values.put("morada_comp", pub_morada_comp);
values.put("porta_n_comp", pub_porta_comp);
values.put("cod_postal_comp", pub_cp_comp);
dataBase.insert("Comprador",values);ยด
this the insert in my class database:
public void insert(String table, ContentValues values) throws SQLException {
mDataBase.insert(table, null, values);
thanks in advance

Related

SQLite file from assets not getting updated after inserting a record

I am trying to insert a record in my database but it's not updating my database.
My insert method:
public void insert(String name, String status, String seat, String id, String pnr) {
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.COL_ID, id);
contentValues.put(DatabaseHelper.COL_NAME, name);
contentValues.put(DatabaseHelper.COL_PNR, pnr);
contentValues.put(DatabaseHelper.COL_STATUS, status);
contentValues.put(DatabaseHelper.COL_SEAT_NO, seat);
database.insert(DatabaseHelper.TABLE_NAME, null, contentValues);
}
Code when inserting:
dbManager = new DBManager(getContext());
dbManager.open();
dbManager.insert("Name","XYZ","B1 21", "2","2348384");
open method:
public DBManager open() throws SQLException {
databaseHelper = new DatabaseHelper(context) {};
database = databaseHelper.getWritableDatabase();
return this;
}
I am using SQLite Asset Helper in my DatabaseHelper and the database is stored in assests/databases
Thank you in advance!
Assets folder is meant for read-only purposes. You cannot alter the contents at run time - except build/development time.
In your case, I would suggest you to copy the database file from assets to your app's private folder (or your desired location) on SD/external storage and then use that database file to perform read/write operations.
In short, your assets' database is good for read operations only.
Note: same rules applies to Raw resources too and they serve a slightly different purpose.

Unable to insert new record in SQLite using Emulators

I am trying to develop a small app where at the end of the flow which needs to insert one row of data in SQLite database.
Following is the code which i am trying for insert:
SQLiteDatabase db = this.getReadableDatabase();
ContentValues values = new ContentValues();
values.put("UserId",1);
values.put("A",scoreValues.get("I"));
values.put("B",scoreValues.get("J"));
values.put("C",scoreValues.get("K"));
db.insert("ScoreTbl",null,values);
db.close();
When the above code is run, i am getting an Error message,
W/FileUtils: Failed to
chmod(/data/user/0/com.abc.xyz/databases/mydatabase.db: :
android.system.ErrnoException: chmod failed: EPERM
Can somebody help me how to overcome this error?
change this..
SQLiteDatabase db = this.getReadableDatabase();
to
SQLiteDatabase db = this.getWritableDatabase();
getReadableDatabase() method Create and/or open a database.
getWritableDatabase() method Create and/or open a database that will be used for reading and writing.
check Docs
the db class extends SQLiteOpenHelper and also create table then after used below code to insert data.
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
values.put("UserId",1);
values.put("A",scoreValues.get("I"));
values.put("B",scoreValues.get("J"));
values.put("C",scoreValues.get("K"));
db.insert("ScoreTbl",null,values);
db.close();

How to update a record in SQLite with Android?

How to update a record in SQLite with Android?
Hello.
I write because I am unfortunately stuck in my project and I hope you can help me.
How do I update a record in a database?
For example in a contact database. In this database of contacts, the following keys are used: name, phone, email and address; my problem is when I need to modify a record, what must I do to modify a record.
I hope you can help me. Thanks in advance.
This is the SQLite code to update the registry:
// Updating single contact
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
values.put(KEY_PH_NO, contact.getPhoneNumber());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?", new String[] { String.valueOf(contact.getID()) });
}
This same code I have found on many websites, but my problem is how to call the code from the MainActivity with Java.
1- create an instance from ur database in the main activity:
DBhelper db = new DBhelper(this);
2- call open:
db.open();
3- set the update method:
db.updateContact(name,phone);
4- close the database connection:
db.close();
5- open and close function in ur database helper class:
public DBhelper open() throws SQLException {
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
6- also you gotta define those as global variables:
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
my advice to you is not to depend on written code you gotta learn to write it ur self specially database creation and usage it is really important so here is a good tutorial for you, you could benefit from it: http://www.tutorialspoint.com/android/android_sqlite_database.htm.
Good luck

Names are getting overlapped in SQLite

In my Android App, I am trying to insert names in a column forcefully into database but In database only last name is showing i.e. Myself because names are overlapping. Actually My database portion is weak.
This is what I am doing in my Database Handler Class,
public void addPoliticianNames()
{
ContentValues values=new ContentValues();
values.put(KEY_POLITICIANNAMES, "I");
values.put(KEY_POLITICIANNAMES, "Me");
values.put(KEY_POLITICIANNAMES, "Myself");
db.insert(TABLE_POLLINGVOTES, null, values);
}
What to do for getting these names sequentially into the column (Here I have to insert names forcefully as I have mentioned above). So, Is there any method for this ? Is there any role of cursor in doing this job (like movetonext method or something like that) ?
Please help me,
Thanks.
you have to make one loop for inserting data into database. it will pick one by one value from array and put it into database.
Put this method in your database class
public void deleteTable()
{
database.delete(TABLE_POLLINGVOTES, null, null);
}
db.deleteTable(); // call it with this line for delete all records of your table
// for inserting data into table.
String[] a = {"I","Me","MySelf"};
ContentValues values = new ContentValues();
for (int i = 0; i < a.length; i++)
{
values.put(KEY_POLITICIANNAMES, a[i]);
db.insert(TABLE_POLLINGVOTES, null, values);
}
Hope it will help you.
If you want to insert three records, you have to use three insert calls.
For example:
ContentValues values=new ContentValues();
values.put(KEY_POLITICIANNAMES, "I");
db.insert(TABLE_POLLINGVOTES, null, values);
values.put(KEY_POLITICIANNAMES, "Me");
db.insert(TABLE_POLLINGVOTES, null, values);
values.put(KEY_POLITICIANNAMES, "Myself");
db.insert(TABLE_POLLINGVOTES, null, values);
#Shiv change the column name for every value
public void addPoliticianNames(String ss)
{
ContentValues values=new ContentValues();
values.put(KEY_POLITICIANNAMES, ss);
}
ss contain value firts time "I",second time "me" like that

Data not inserting in Sqlite database in android

I am developing an application which requires use of an SQLite database. I have implemented fetching the data, but I am facing problems when I try to insert data. The problem that I am having is that the new data I enter is not stored, i.e nothing is new is being entered into the database.
This is my insert code, myDataBase is an instance of SQLiteDatabase.
public void insertTitle(String Recipe)
{
ContentValues initialValues = new ContentValues();
initialValues.put(COLUMN_NAME,value);
myDataBase.insert(ZRECIPE, null, initialValues);
}
Try it this way. You need first start transaction, then mark it as successful and end.
try {
myDataBase.beginTransaction();
myDataBase.insert(ZRECIPE, null, initialValues);
myDataBase.setTransactionSuccessful();
}
finally {
myDataBase.endTransaction();
}
You forgot to commit the transaction.
Try with this code this may help you
public void insertTitle(String Recipe)
{
ContentValues initialValues = new ContentValues();
initialValues.put(COLUMN_NAME,Recipe);
myDataBase.insert(ZRECIPE, null, initialValues);
}

Categories

Resources