I am developing Android app and I have SQLite database in it. I have inserted records in database. Now when I am sending my app into another device there is empty database!. Please help me
public void createPrincipal(CollegePrincipal principal){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(PRINCI_NAME, principal.getName());
values.put(PRINCI_QUALI, principal.getQalification());
values.put(PRINCI_INTRO, principal.getIntro());
// insert row
long princi_id = db.insert(TABLE_PRINCI, null, values);
Log.e("princi id", String.valueOf(princi_id));
}
when ever you create data base it will in the local store of your device , And when you transfer app it wont care you local data you need store those data variables in you code to get in the another device.
Related
I am working in an android studio project and I have a login activity,I created database using SharedPreferences and SQLite;I have created a table which has columns id,email,password,check_in,check_out.
I made it possible to login after I register a user;but I do not know how to insert a data in check_in cell of logged user.
So my problem is that how to make possible to add the time of check_in in table in database after logged in?
And how I can the see the table created using SQLite?
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("check_in", value);
contentValues.put("check_out", value);
// this will insert if record is new, update otherwise
db.insertWithOnConflict(TABLE, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
I made it possible to login after I register a user;but I do not know
how to insert a data in check_in cell of logged user.
You would use the SQLiteDatabase's update method something along the lines of :-
ContentValues cv = new ContentValues();
cv.put("check_in",System.currentTimeMillis());
SQLiteDatabase db = this.getWritableDatabase();
db.update("your_table_name",cv,"email=? AND user=?",new String[]{email,user});
And how I can the see the table created using SQLite?
You can
install a product that provides this ability,
copy the database via adb or tools that utilise adb, so that a product that can look at SQLite databases can access the copied file,
extract a cursor from the table and then use this as the source for displaying the data e.g you could use something like
:-
SQLiteDatabae db = this.getWriteableDatabase();
Cursor csr = db.query("your_table_name",null,null,null,null,null,null);
Databaseutils.dumpCursor(csr);
csr.close();
Inserting data in SQlite from EditText
Code was working well before i wiped out data from emulator now its giving me Null Pointer Exception, Kindly figure where actual problem lies. Thanks
Data is being inserted from here ]
public long insert(String name, String password, String role, String unique) {
sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_USERNAME, name);
contentValues.put(KEY_PASSWORD, password);
contentValues.put(KEY_ROLE, role);
contentValues.put(KEY_UNIQUE, unique);
long result = sqLiteDatabase.insert(DOCTOR_TABLE,
null,
contentValues);
sqLiteDatabase.close();
return result;
}
SQLite Database class
Well. It's an obvious.
As you mentioned you have wiped the data, so the table which is located in the android app's own data folder will be deleted too. So you must check whether it exists or not.
Call to Getting your table returns null.
So you inserting your value to nothing.
So You can just alter the code to see if the table exists and if not create it.
Basically you just call,
CREATE TABLE IF NOT EXISTS method(SQLite 3.3 and above support IF NOT EXISTS). When you first call it, if not existed, it will create that table but later it will return the existing table with same name.
In one line- check or get the correct table before you do any operation.
I have an app which is online on play store. It comes with a preloaded database, which is bundled inside assets folder and when user initially launches the app, db is copied to preferred folder.
There is a table of favourites , in which user stores there bookmarked/favourite records.
I have new data and few other modifications which I have made with bundled database, and will be uploading new APK to play store.
My concern is that, users who are currently using my app, they will get update notification and once they update, their app will use new bundled database, and they will lose their records stored in favourites.
I did some google search on it, and SQLiteHelper onUpgrade() works when we are creating database on runtime, but in my case its pre-loaded bundled database.
How can I backup the favourites data before update and then load it back in new db file.
Thanks
In the newer versions, you should preserve the table data that you want to keep. To do this, you could:
Make a copy of the old database file into a backup.
Extract the newer database file to the working directory
Open two SQLiteDatabase objects, one for each database file.
Copy all data from the tables you want to preserve.
Delete the backup of the old file if everything was successful.
For step 4 you shouldn't even need to code specifically, it can be done for every table, for example more or less like this (warning, this code is untested):
static void copyTable(SQLiteDatabase source, SQLiteDatabase destination, String tableName)
{
Cursor c = source.query(tableName, null, null, null, null, null, null);
destination.beginTransaction();
try
{
String[] columns = c.getColumnNames();
ContentValues insertValues = new ContentValues();
while (c.moveToNext())
{
insertValues.clear();
for (int i = 0; i < columns.length; i++)
insertValues.put(columns[i], c.getString(i));
destination.insert(tableName, null, insertValues);
}
destination.setTransactionSuccessful();
}
finally
{
destination.endTransaction();
}
c.close();
}
I have an android application which is using Sqlite as database.It has following tables:
Hotels
Locations
Favorites
I am keeping my raw database file in assests folder and when user installs my app i just copies this database to /data/data/package_name/databases directory.Initially Favorites table is empty and it gets populated after user start liking hotels.My problem is that I want to launch my updated version of app with some bug fixes and some new hotels added to the database, so I need to update database of existing users with new hotels and locations without affecting the favorites table.Now if I keep my old approach and update the Database Version Number then application will remove the old database and use the new database but all data in favorites table will be lost.I don't want it to happen.Now problem is how do I update Hotels and Locations table without loosing data in Favorites table.
I know this question was asked long ago, but I had a similar issue and wanted to share my solution, seems to do the trick for me. I'm a novice so feel free to give input-
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//code to keep table data
List<obj> objList = new ArrayList<obj>();
String selectQuery = "SELECT score,list_name,quiz_length FROM obj_table";
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
obj o = new obj();
o.final_score = cursor.getInt(0);
o.quiz_name = cursor.getString(1);
o.quiz_length = cursor.getInt(2);
objList.add(o);
} while (cursor.moveToNext());
}
//done storing data, now upgrade DB from asset file
try {
//my db file is upgraded here
copyDataBase();
} catch (IOException e) {
}
//now insert our saved table data
for (Score obj_rec: objList){
ContentValues values = new ContentValues();
values.put("score", obj_rec.final_score);
values.put("list_name", obj_rec.quiz_name);
values.put("quiz_length", obj_rec.quiz_length);
db.insert("obj_table", null, values);
}
}
Before updating write the contents of you previous table to a file and save it on the sdcard.
Then you may update your database with new version.
And after doing that copy back the data from the backup file(from sdcard) to the updated database. After the successful copying of the backup, delete the file from the sdcard.
Usualy upgrade a database has to be done with SQLiteOpenHelper class. I would advise You to do some tests at Your own device before publish it. You have to increment Your Database Version and call "ALTER TABLE" method from sqlite. This has been discussed in many threads here, the clearest one I think is this one:
Difficulty in upgrading SQlite table
and here is even a article with some solution:
http://joshhendo.blogspot.de/2012/03/android-sqlite-database-upgrade.html
However, a safe way would be to save the old database in a tempfolder, that the user can get back the old one if anything is running into chaos.
I need to store list of contacts into sqlite database. I've displayed contacts in my project application using Contacts Content Provider. I just need to store the list from emulator into database. Anyone knows how? please help thanks.
Not a big deal.
Transfer your ContactModel into a ContentValue:
ContentValues values = new ContentValues();
values.put("name", name);
values.put("number", number);
then insert it in database :
database.insertOrThrow(databaseTable.getTableName(), null, values)
But keep in mind that it will always be as fast to get a contact from SQLite database as from the ContentProvider since it's pretty the same tech.