When I am trying to insert to data base log cat shows an error like java.lang.illegalstateexception database not open android.
But I have opened the db using
db = SQLiteDatabase.openDatabase(DATABASE_PATH, null,
SQLiteDatabase.OPEN_READWRITE);
The error is not occurring frequently.Anybody know the reason for this?
Try it like this:
if (!db.isOpen()) {
db = getApplicationContext().openOrCreateDatabase(DATABASE_PATH, SQLiteDatabase.OPEN_READWRITE, null);
}
try
DatabaseHelper dataHelper;
SQLiteDatabase mDb;
public DbManager openDB() throws SQLException {
mDb = dataHelper.getWritableDatabase();
return this;
}
and call this method where your re writing your current code.
Try this code
public class DataBaseHelper extends SQLiteOpenHelper{
public SQLiteDatabase openDataBase() throws SQLException{
//Open the database
File dbFile = _myContext.getDatabasePath( DB_PATH + DB_NAME );
_myDataBase = SQLiteDatabase.openDatabase(dbFile.toString(), null, SQLiteDatabase.OPEN_READWRITE);
return _myDataBase;
}//end of openDataBase() method
}
maybe you are opening an opened Database. close your database every time your work is done.
Related
The database i want to import is over 5Mb. When i put the .db in asset folder i got some errors. So i split the database into smaller db files and assemble the pieces to create the database. So is there any way i can import the database from sdcard and start using the db without splitting and assembling?
Thanks in advance.
Yes, there is:
place the database file inside the sdcard then create a method inside a class, the method is like:
public class DB_Path {
public final SQLiteDatabase getOrders() {
File dbfile = new File("/sdcard/TheDataBaseFile");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
return db;
}
}
-----------------------
initialize like
public DB_Path dbp = new DB_Path();
public SQLiteDatabase db = dbp.getOrders();
after that you can call the cursor with db.
Cursor cur = db.rawQuery("the sql query",null);
i encountered a little problem with my current project. I am doing an android application which needs to connect to a SQLite database to work through some statements. I believe the statements etc are fine, my only problem is the fact that the connection to the database is not succesfull.
LogCat Error:
04-18 08:20:30.688: E/Database(304): sqlite3_open_v2("jdbc:sqlite:res/raw/randomdb.db", &handle, 1, NULL) failed
So my code so far for connecting to the database is like this:
String url = "jdbc:sqlite:res/raw/randomdb.db";
SQLiteDatabase db;
db = SQLiteDatabase.openDatabase(url, null, SQLiteDatabase.OPEN_READONLY);
As you can see, i am trying to acces a database which is located in my project/res/raw folder. Does anyone see the mistake?
!!!UPDATE!!!
*I tried to go the way with SQLiteOpenHelper, but still encouner an error i cannot seem to solver. Here is my new code:*
public class DatabaseAdapter extends SQLiteOpenHelper {
private static String dbPath= "data/data/com.YourPackageName/applicationDb/";
private static String dbName = "YourDBName";
private SQLiteDatabase applicationDatabase;
private final Context applicationContext;
private boolean checkDataBase(){
File dbFile = new File( dbPath + dbName);
return dbFile.exists();
}
public void openDataBase() throws SQLException{
String fullDbPath= dbPath + dbName;
applicationDatabase = SQLiteDatabase.openDatabase( fullDbPath,null,SQLiteDatabase.OPEN_READONLY);
}
#Override
public void onCreate(SQLiteDatabase db) {
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
I get this error:
Implicit super constructor SQLiteOpenHelper() is undefined for default constructor. Must define an explicit constructor
Any ideas? Would be great!
if you want to connect your android application with SQLite database then you need to extends SQLiteOpenHelper
public class AbcClass extends SQLiteOpenHelper
after that you need to Override these method:
onCreate and onUpgrade
and constructor of AbcClass looks like:
public AbcClass(Context context) {
super(context, DB_NAME, null, VERSION); //public static final String DB_NAME = "test.sqlite";
}
public boolean databaseExist()
{
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
This is the solution...
OR-----------------------
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
I think the path you specify is not the correct one and so as mentioned in one of the other answers the database is not found. However I had never used the method openDatabase to read from the raw folder so I do not know which is the correct path.
However once upon a time I had shipped the database along with my application. It resided in the assets folder and once the application started I copied it in the private application storage (much like any database created with SqliteOpenHelper). From then on I used the usual way with SqliteOpenHelper to access the database.
Basically for this I followed the blog mentioned in this thread and because my database file was bigger than 1MB I used the technique described in this thread. Hopefully combining those two you will get your database running!
EDIT Btw you are wrong, openDatabase does not accept url, but path.
I am trying to figure out what order I need to do everything to have my program flow well and not crash. I want the player of my game to have a team of three people each as their own class with stats such as attack, health, etc.
When the game is first installed and run, I want to have a sqlite db made and filled with the 3 starter characters.
When they start it any other time, I want to have the three stored characters in the DB to be assigned to 3 local character objects.
I have a SQLite Adapter class handling the Db but I'm not sure how to order everything so that I check first to see if there is a Db in the programs Db path and open it if there is. Otherwise just create a new one and populate it with the initial characters.
I currently have:
DbAdapter = new PlayerDbAdapter(this);
DbAdapter.open();
DbAdapter.setInitialPlayers(); // inserts my initial player object info into the Db
which calls
public PlayerDbAdapter open() throws SQLException {
dbHelper = new SummonSQLiteHelper(context);
String s = DbPath + DB_NAME;
database = SQLiteDatabase.openOrCreateDatabase(s, null);
return this;
}
And now I'm a little lost as to what I need to make sure that I don't overwrite any saved info in the Db with the initial values everytime I start the program. Any suggestions?
Try like this
public PlayerDbAdapter open() throws SQLException {
dbHelper = new SummonSQLiteHelper(context);
String s = DbPath + DB_NAME;
bool b=checkDataBase(s);
if(b==false)
{
database = SQLiteDatabase.openOrCreateDatabase(s, null);
setInitialPlayers(); <---- set initial values here
}
else
{
database = SQLiteDatabase.openOrCreateDatabase(s, null);
}
return this;
}
private boolean checkDataBase(String str) {
SQLiteDatabase checkDB = null;
try {
checkDB = SQLiteDatabase.openDatabase(str, null,
SQLiteDatabase.OPEN_READONLY);
checkDB.close();
} catch (SQLiteException e) {
// database doesn't exist yet.
}
return checkDB != null ? true : false;
}
I have a table login2 in /data/data/sankalp.jain.shre/databases/loginfinal.db.
The database has been created correctly which i have verified myself using adb sqlite3 and querying the table.
Inserting from commandline works but using rawQuery,it seems data is not getting added(cant get it on commandline too).
private SankalpDB dbhandle; //SankalpDB extends SQLiteOpenHelper
private SQLiteDatabase sqdb;
..............
public void open() throws SQLException {
System.out.println("Opening"); //This also gets printed.
sqdb = dbhandle.getWritableDatabase();
}
public void addUser()
{ System.out.println("Adduser");
System.out.println(sqdb.getPath()); ///prints /data/data/sankalp.jain.shre/databases/loginfinal.db
sqdb.rawQuery("Insert into login2 values('ax13','Kate','password');", null);
sqdb.rawQuery("Insert into login2 values('cax56','Natalie','passswd');", null);
....
...
sqdb.close();
}
The logcat output is not giving any error either.What am I doing wrong here.??
Try and do the following:
Cursor c = sqdb.rawQuery("Insert into login2 values('ax13','Kate','password');", null);
c.moveToFirst();
c.close();
Maybe you can try with the extended form
sqdb.rawQuery("Insert into login2 (alias,user,password) values('ax13','Kate','password');", null);
where alias, user and password are the DB fields
I need to check existing database before creating new database on android 2.2. How to check it?
use openOrCreateDatabase method
Read here
----- EDIT ------
public boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
Doesn't it work with the DatabaseHelper ?
If you haven't tried here is code I posted before...
Android - Sqlite database method undefined fot type
You should need to check database already exists or not, if not than create database else not create database. Please you can use below query.
CREATE TABLE if not exists TABLE_NAME (key data_type);
Call this query inside onCreate method.
To check if your database was created you can use the following code and it will not be recreated every time you open the application.
dbName = is the name of you DB
public static boolean doesDatabaseExist(Context context, String dbName) {
File dbFile = context.getDatabasePath(dbName);
return dbFile.exists();
}