考虑我有一个包含两个表A和B的数据库。在某个时间我想检查表A是否已经在数据库中创建.. plz帮助我如何找到某个表是否存在在数据库?
提前致谢。
答案 0 :(得分:2)
SELECT * FROM sqlite_master where type='table' and name='my_table_name'
答案 1 :(得分:0)
您可以像这样发出SQL查询:
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
此表包含DB中所有表的列表。只需添加另一个名字。
答案 2 :(得分:0)
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() {
}
}
答案 3 :(得分:0)
一个非常简单的解决方案。不确定它的可信度。 在http://www.sqlite.org/sqlite.html查看sqlite3的特殊命令部分 .tables A是你发给sqlite的命令。 在SQLiteDatabase类中使用rawQuery方法。 如果没有帮助的话,Plz就不会这样做。
Cursor myCursor = mySQLiteDatabase.rawQuery(".tables A", null);
if(myCursor.moveToFirst()){
//table exists
} else {
//table does not exist
}