Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get database path

Im new to android and I want to create kind of CRUD with SQLite.

I have a public class DataBaseHelper which have a constructor:

public DataBaseHelper(Context context) {     this.context = context; } 

and a getter for datebase:

public SQLiteDatabase getDataBase() {     if (dataBase == null) {         try {             dataBase = context.openOrCreateDatabase(DataBaseHelper.dbName,                                                     Context.MODE_PRIVATE, null);              dataBase.execSQL("CREATE TABLE " + DataBaseHelper.accountTblName + " " +                                         "(ID integer PRIMARY KEY AUTOINCREMENT" +                                         ",name TEXT" +                                         ",address TEXT" +                                         ",password TEXT)");             }             catch(Exception e) {                 Log.e(context.toString(), e.getMessage());                  if (dataBase != null) {                     dataBase.close();                 }             }     }            return dataBase; } 

If I start a program for the first time - everything seems to be okay. database is null and I create it (with a table). But when I restart my app database is null again and table cannot be created.

I've decided to write a checkDB function, which tries to open a database. But where can I get a path for it? I dont want it to be "hardcoded".

Or may be I'm doing some kind of anti-pattern?

like image 684
user1284151 Avatar asked Mar 21 '12 18:03

user1284151


People also ask

How do I find my database path?

getDatabasePath("YourDbName"); String db_path=path. getAbsolutePath(); Log. i("Path:",db_path); even you will see the both code ...then both code is same, 1st one is the shortcut method to get the database path, and it's occupy the less memory compare to second option.

How do I view a DB file?

In Windows Explorer, navigate to the drive or folder containing the Access database file you want to open and double-click the database. Access starts and the database is opened.

What is database extension?

A DB file is a generic database file. It stores data in a structured format, usually comprised of a set of tables, table fields, field data types, and field values. Many applications create and use different, application-specific types of DB files.


2 Answers

You can get path by context.getDatabasePath(DataBaseHelper.dbName)

like image 113
Ethan Liou Avatar answered Oct 08 '22 21:10

Ethan Liou


To get the path of Sqlite data base

here I am writing two ways to get the path.

(1) using custom method

public String getDbPath(Context context,String YourDbName) {     return context.getDatabasePath(YourDbName).getAbsolutePath(); } 

(2) using File class

  File path=context.getDatabasePath("YourDbName");   String db_path=path.getAbsolutePath();   Log.i("Path:",db_path); 

even you will see the both code ...then both code is same, 1st one is the shortcut method to get the database path, and it's occupy the less memory compare to second option.

like image 31
Abdul Rizwan Avatar answered Oct 08 '22 22:10

Abdul Rizwan