How can we create a database in android using the SQLiteDatabase class? - android

I want to create database in android without usin the SQLiteOpenHelper Class. I want to create it using the SQLiteDatabase class.I am doing it in this way--
SQLiteDatabase sqldb;
String path="data/data/mypackagename/sample.db"
public void CreateDatabase(){
sqldb.openOrCreateDatabase(path, null);
}
While executing it's throwing an exception that 'unable to open the databse file'. Please help me out in creating the database file using the SQLiteDatabase class.

You can not give path of your database like this ...here is the code for it..
SQLiteDatabase db;
db = openOrCreateDatabase(
"TestingData.db"
, SQLiteDatabase.CREATE_IF_NECESSARY
, null
);
here is the link that you can refer..http://www.higherpass.com/Android/Tutorials/Accessing-Data-With-Android-Cursors/

Firstly, I'm assuming your sample.db file is correctly formatted (if not, there are several very good resources for sqllite, and Deepak referenced one). Secondly, please check your permissions. If you were previously writing to your file, then you know you're fine. Just to be safe, chmod it. If that's still not it, take a look at this:
http://www.pantz.org/software/sqlite/unabletoopendbsqliteerror.html

Related

How to Close SQLite Cursor and other open SQLite database?

I am trying to write a code in Android and going through following errors. I am opening a SQLite database and using Cursor.
Java Code
for(int y3=0;y3<10;y3++)
{
String sql_query5= "SELECT * FROM Data WHERE Id="+y3+"";
Cursor cur5 = SQLiteDatabase.openDatabase(db, null, SQLiteDatabase.OPEN_READWRITE).rawQuery(sql_query5, null);
if(cur5.moveToNext())
{
//
}}
So i need to display the Age in the layout but i am facing SQLiteCantOpenDatabaseException and Database object not closed. I don't understand the errors.
Please let me know some solution and suggestion !!!
Do not use hard-coded file paths. You have no guarantees about the structure of the filesystem.
Learn to use SQLiteOpenHelper. This is what helps you create and maintain your SQLite database. Instead of trying to open the database yourself, you create a new SQLiteOpenHelper and call getReadableDatabase().
When you are done reading from a Cursor, call cursor.close().
If you want to close the database, you can call close() on it as well; just be aware that any cursors you got by querying it will no longer be able to give you data, so make sure you are finished them those before closing the database.
add '' in your
String sql_query5= "SELECT * FROM Data WHERE Id='"+y3+"' ";

How to check if a Sqlite database exists in android?

I'm pretty new using ORMLite with android, and I read in this question that the method onCreate() of the class OrmLiteSqliteOpenHelper is not called when the database already exists... but I'd like to confirm it by myself, does anyone know how to check if the database exists?
It looks like OrmLiteSqliteOpenHelper inherits the getWritableDatabase() method from SQLiteOpenHelper. If that method returns null, then the database does not exist.
You can also create a database by using SQLiteDatabase
for example
SQLiteDatabase = StackOverFlowDB;
//Creating database
StackOverFlowDB = MainActivity.this.openOrCreateDatabase("StackOverFlow", MODE_PRIVATE, null);

call sql from another class for android app

Hi
I have created a database for my application and i have added items to the database using methods from the database class. I am encountering a problem do when i try to execute a sql query in the other class(app.java), i need to reference a database and thats where im having the problem!
this is the sql query im trying to execute(in database.java)
public void getData(SQLiteDatabase db, String data){
String sql =
"SELECT permissions FROM genres WHERE name = "+data+";";
db.execSQL(sql);
}
and this is how i am calling it(in app.java)
appData.getData(db, chosenGenre);
I just dont no what to put for the "db" part in appData.getPermissions(db, chosenGenre);
Does anyone know how to do this?
Thanks
You would typically use a SQLiteOpenHelper to create the database files (if necessary) and obtain a SQLiteDatabase object which is used to access the actual database (files on disk read by SQLite).
http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html
Here's a nice tutorial
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/2/
You don't show the code where you define the database manager 'db'! If you are using a database created outside the android device, you might get some help from this link:
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
Also, I have found that it's best to frame all table names and supplied values in single quotes a la -
String sql = "SELECT permissions FROM 'genres' WHERE name = '" + data + "'";
Note that the semicolon is not required.

Problem in Android Database creation. Please help

I am new to android. I implemented these steps to create a sqlite database,
public void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.main);
SQLiteDatabase db;
db = openOrCreateDatabase("TestData.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
}
but the the database not made i searched the database at every location (in packcage also), also checked with commands specified.
# ls /com/testproject/action.TestAction/databases
I don't know why you are using this commands here but Android stores the database files in the directory:
/data/data/your_app_package_name/databases/
I assume if your application have com.testproject pakage name in your AndroidManifest.xml file then your database file path should be like this:
/data/data/com.login/databases/TestData.db
So just insure that your db file is there or not.
Refer this : http://developer.android.com/guide/developing/tools/adb.html#sqlite
Check out this tutorial provided by Android Developers. It contains some useful information on general Android things, including how to create an SQLite DB
http://developer.android.com/resources/tutorials/notepad/index.html
The SQLite stuff is in Exercise 1.

How to access an existing sqlite database in Android?

So far we have developed apps in android that create database on runtime. We like to know how can we access a pre-built or existing database/sqlite file in our android app? Please provide detail
Take a look at the documentation for android.database.sqlite.SQLiteDatabase.
In particular, there's an openDatabase() command that will access a database given a file path.
SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, 0);
Just specify the path to your database as the path variable, and it should work.
I have followed the same road and found two issues:
Include the _id field on any table you create and make sure it is
part of the columns to return when you query any table.
You may run into an error like Failed to open the database. closing
it.. This is because we are copying the database from an existing
file instead of creating tables on top of a db created by the sqlite
android API and the table android_metadata may not exist. For
creating it just run the following query on your db:
CREATE TABLE android_metadata (locale TEXT);
And add the corresponding locale, for example en_US
First copy your SDCARD and give it's path in the variable "DBNAME" in the following example.
it will be something like "/sdcard/persons.db" if you are directly pulling it to sdcard.
Use this for accessing database in application :
Context context = getApplicationContext();
context.getDatabasePath(DataBaseHelper.database_Name);

Categories

Resources