Reputation: 21
Consider am having a database which contains two tables A and B. On a certain time i want to check whether table A is already created or not in the database.. plz help me how to find a certain table is present or not in databse ?
Thanks in advance.
Upvotes: 0
Views: 977
Reputation: 2196
A very simple solution. Not sure of its credibility. Take a look at the Special Commands to sqlite3 section at http://www.sqlite.org/sqlite.html .tables A is the command that u hv to issue to sqlite. Use the rawQuery method in SQLiteDatabase class. Plz do lemme no if it doesn't help.
Cursor myCursor = mySQLiteDatabase.rawQuery(".tables A", null);
if(myCursor.moveToFirst()){
//table exists
} else {
//table does not exist
}
Upvotes: 0
Reputation: 11775
SELECT * FROM sqlite_master where type='table' and name='my_table_name'
Upvotes: 2
Reputation: 9479
public class DatabaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = "ur path";
private static String DB_NAME = ur db name;
private static SQLiteDatabase myDataBase;
private static DatabaseHelper databaseHelper = null;
private final Context myContext;
private static boolean doCopyDatabase = true;
private DatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
myContext = context.getApplicationContext();
}
/**
* To check whether the databaseHelper and the database is existing or not.
* If databaseHelper is existing, a function is called to open the database.
* If there is no database, new database is created. If databaseHelper in
* not existing, new databaseHelper is created.
*/
public static DatabaseHelper getInstance(Context context) {
if (databaseHelper == null) {
databaseHelper = new DatabaseHelper(context.getApplicationContext());
databaseHelper.openDataBase();
if (myDataBase == null) {
try {
myDataBase = databaseHelper.getWritableDatabase();
if (doCopyDatabase) {
databaseHelper.copyDataBase();
} else {
databaseHelper.runSchemaCreator();
}
} catch (Exception e) {
Log.e("Exception in DatabaseHelper: getInstance()", e
.getMessage());
}
databaseHelper.openDataBase();
}
}
return databaseHelper;
}
/**
* @param doCopyDatabase the doCopyDatabase to set
*/
public static void setDoCopyDatabase(boolean doCopyDatabase) {
DatabaseHelper.doCopyDatabase = doCopyDatabase;
}
private void runSchemaCreator() {
TablesCreator tablesCreator = new TablesCreator(myDataBase);
try {
tablesCreator.createTables();
} catch (SQLiteException e) {
Log.i("Error in DatabaseHelper : createTables()", e.getMessage());
}
}
/**
* Returns the database.
*/
public SQLiteDatabase getDatabase() {
return myDataBase;
}
/**
* Opens the database.
*
* @return
* @throws SQLException
*/
private boolean openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
try {
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
Log.i("Exception in DatabaseHelper : openDatabase()", e
.getMessage());
}
return myDataBase != null ? true : false;
}
@Override
public synchronized void close() {
if (myDataBase != null) {
myDataBase.close();
myDataBase = null;
}
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public class TablesCreator {
private SQLiteDatabase myDataBase;
public TablesCreator(SQLiteDatabase db) {
this.myDataBase = db;
}
public void createTables() {
}
}
Upvotes: 0
Reputation: 12366
you can issue SQL query like this one:
SELECT name FROM sqlite_master
WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'
UNION ALL
SELECT name FROM sqlite_temp_master
WHERE type IN ('table','view')
ORDER BY 1
This table has a list of all tables in DB. Just add another where with your name.
Upvotes: 0