no database on my android phone - android

I am attempting to create a database in my Android app. It has worked, but when I restart the emulator I receive an error that indicates that my table does not exist. I have found that my "OnCreate" is not started and there is no directory on my sd card of my app? Can you help me find my mistake?
My code is:
public DatabaseVerwerker(Context context) {
super(context, database_naam, null, database_version);
}
public void onCreate(SQLiteDatabase db)
db.execSQL("CREATE TABLE IF NOT EXISTS variabel (key TEXT, value TEXT);");
)
And will the database still exists after close and open the app?
Greatings,
I hope it's my last post of android. it get's to mutch

Make sure your database has been created before you do your operation on database. The better way to create a database only used by your application is using openOrCreateDatabase. Here is the example.
CursorFactory cursorFactory = new CursorFactory() {
#Override
public Cursor newCursor(SQLiteDatabase db, SQLiteCursorDriver masterQuery,
String editTable, SQLiteQuery query) {
return null;
}
};
openOrCreateDatabase(DBNAME, MODE_PRIVATE, cursorFactory, new DatabaseErrorHandler() {
#Override
public void onCorruption(SQLiteDatabase dbObj) {
}
})

Thanks it works again. i must build the onUpdate function and trigger it.

Related

How to make onUpgrade() statements run even on first installation?

In my application i have a database which is created using DB queries in the DBHelper class. I want the onUpgrade statement to run on installation of the apk as there is a possiblity that the user might unistall and install the same application.
In this case it becomes the first installation on the device and the onUpgrade does not run which causes the application to crash.
It works perfectly if the new app is updated on the previous app.
How do i reslove this?? What is solution for such cases??
Please help !! Thanks in Advance !!
Here is my DbHelper.java
public class Dbhelper extends SQLiteOpenHelper {
public Dbhelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(TABLE_CREATE_IMAGE);
database.execSQL(TABLE_CREATE_ATTEDANCE);
database.execSQL(TABLE_CREATE_STOCK);
}
#Override
public void onUpgrade(SQLiteDatabase database, int oldVersion,
int newVersion) {
Log.e("","upgrade ");
database.execSQL("ALTER TABLE stock ADD db_stock_id varchar(100)");//1.1
//
}
public void dropandcreate(SQLiteDatabase database)
{
database.execSQL("DROP TABLE IF EXISTS image");
database.execSQL("DROP TABLE IF EXISTS attendance");
database.execSQL("DROP TABLE IF EXISTS stock");
onCreate(database);
}
}
Call it yourself:
#Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(TABLE_CREATE_IMAGE);
database.execSQL(TABLE_CREATE_ATTEDANCE);
database.execSQL(TABLE_CREATE_STOCK);
onUpgrade(database, VERSION);
}

Sharing database between android native code and phonegap code

I have an app which consists of a common database accessed from native code and phonegap code of the same app, however, I am not able to access data written into the db by the native code in the phonegap code. I am using following code snippet for android -
public class DataBaseHandler extends SQLiteOpenHelper {
public DataBaseHandler(Context context) {
super(context, "dbName", null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void addNotification(String data) {
SQLiteDatabase db = this.getWritableDatabase();
values.put("data", data);
db.insert("myTable", null, values);
db.close();
}
but when I try to accces the data from "myTable", I dont get any data and the table is empty, I have checked the ddms to ensure that data is populaated in the table, I am using the sqlite plugin for phonegap on android (https://github.com/pgsqlite/PG-SQLitePlugin-Android)
I have this doubt that could it be possible that native and phonegap parts are somehow using the different database and not the common database.
If you read code from https://github.com/litehelpers/Cordova-sqlite-legacy-build-support/tree/master/src/android/io/sqlc. The plugin code does not use SQLiteOpenHelper class to create sqlite database. The plugin is using another method SQLiteOpenFlags to create of database table. I have same problem like you also.

Android Database

I am designing an application for an online exam. And I created a database using SQLite Browser and pulled it to Eclipse. In an emulator its working fine; it is able to retrieve and store data. But the problem comes when I place the .apk file on the mobile. On the mobile it's unable to retrieve the existing database. I am unable to bind the database file along with the .apk file, even after placing it in the assets folder.
Can anyone help?
Details:
Registration module
User Test module (display the questions from database)
Score submission module
How are you querying on database ?
The correct way is not to place a separate db in your code, but to create one dynamically. For e.g. the following code :
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DB_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSql("/*Put Create Table sqls here*/");
//onCreate will be called only once( when db doesn't exists for application, it creates here with the code)
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Use a database helper class like this in your code.
Now whenever you want to query on your db you can do it like this :
dbHelper = new DatabaseHelper(ctx);
db = dbHelper.getWritableDatabase();
//Start querying on db.(if it is not created oncreate() of dbhelper will create it for you.
So simply put your initial db create,/insert statements in oncreate() of dbhelper

App Crashes due to Db creation on every launch

I have an application in which i am trying to create a Database. On the first run the application works properly. Subsequent launch of the application it crashes. The error is because the application is trying to create the db again.
So i want to know how to create a database or tables only if they dont exists or only if the application is run the first time.
You can extend the class SQLiteOpenHelper to handle different versions of your database. Here is an example:
public class MyDbOpenHelper extends **SQLiteOpenHelper** {
final static int VERSION = 2;
public MyDbOpenHelper(Context context, String dbname) {
super(context, dbname, null, VERSION);
}
#Override
public void **onCreate**(SQLiteDatabase db) {
/* TODO SQL-Queries for new Database */
}
#Override
public void **onUpgrade**(SQLiteDatabase db, int oldVersion, int newVersion) {
/* SQL-Queries t*/
if (oldVersion < 2) {
db.execSQL("ALTER TABLE my_table ADD COLUMN new_attrubte INTEGER");
}
}
}
Handle of database is new in onCreate and if it exist, onUpgrade is called with existing version number in oldVersion parameter.
SQLite has a CREATE TABLE IF NOT EXISTS command. You could try that.
It is usually an error to attempt to create a new table in a database that already contains a table, index or view of the same name. However, if the "IF NOT EXISTS" clause is specified as part of the CREATE TABLE statement and a table or view of the same name already exists, the CREATE TABLE command simply has no effect (and no error message is returned). An error is still returned if the table cannot be created because of an existing index, even if the "IF NOT EXISTS" clause is specified.

Creating initial SQLiteDatabase when app is installed

I am writing an app that displays fun-facts (and the source they are from). The user can browse through the facts one by one.
Here is the design I thought of :
Create a table in SQLiteDatabase with a text column that stores the fun-fact and a second column that stores it's source. (not sure if this is the best way to go about doing it, but I want the app available even without the network)
My question is, when database is initially created on the device, should I manually populate the database from within the code, something like this pseodo-code:-
#Override
public void onCreate(SQLiteDatabase db) {
//Create the table
//Populate the table
//Insert statement 1
//Insert statement 2
//Insert statement 3
...
//Insert statement 500
}
Surely there must be a better method to create the initial database when the app is installed?
Are you certain that you really need a databse? Doesn't it just add unnecessary overhead to something so trivial?
Can't you just declare the array in your code, or am I missing something? Whether it's in the db or your code, it is taking up space. The db will add some overhead to that and vious?will take some time to load, plus your code has to handle errors, etc.
Woudl you not be better off with a simple array declared in your code? Or am I misisng something obvious? (maybe users can d/l a new db? But is that so much more overhead than d/ling a new program?)
If I'm way off, please explain (rather than downvoting). I am trying to help
Edit: presumably you already have your facts soemwhere? Maybe in a text file? You could just write code to parse that and initialze and array (or populate a db). It should bascially be a short for loop.
use a class derived from SQLiteOpenHelper
i already wrote sth obout this on my blog www.xenonite.net
public class myDatabase extends SQLiteOpenHelper
{
private static final String DB_NAME = "database.db";
private static final int DB_VERSION = 1;
public MyDatabase(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE tbl_test ( id INTEGER PRIMARY KEY AUTOINCREMENT, test TEXT NOT NULL )");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS tbl_test");
onCreate(db);
}
}
you can use this like
myDatabase db = new myDatabase(getApplicationContext());
sql = "INSERT INTO tbl_test (test) VALUES ('xyz')";
db.getWritableDatabase().execSQL(sql);
String sql = "SELECT id FROM tbl_test";
Cursor result = db.getWritableDatabase().rawQuery(sql, null);
int value;
while(result.moveToNext())
{
value = result.getInt(0);
}
on every call to db, myDatabase.onCreate() is called, so your database will be created on the first run. when changing the table structure, implement onUpgrade() and increment DB_VERSION

Categories

Resources