我是 Android 编程新手,所以请帮助我。我已经使用 SQLite 浏览器创建了数据库,我尝试将我的应用程序与此数据库文件连接,但它不起作用,错误日志显示“没有这样的表:datasaya”。我创建的数据库中只有 1 个表,怎么会发生这种情况。
这是我的 sqlite 辅助类:
public class SQLiteHelper extends SQLiteOpenHelper {
private static String DB_PATH="/data/data/com.example.latihandbsqlite/databases/";
private static String DB_NAME = "cekdatabase.sqlite";
private static int VERSION =1;
private SQLiteDatabase myDataBase;
private final Context myContext;
public SQLiteHelper(Context context) {
super(context, DB_NAME, null, VERSION);
myContext = context;
try {
createDatabase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
}
public void createDatabase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
System.out.println("DB EXIST");
} else {
this.getReadableDatabase();
this.close();
copyDataBase();
}
}
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath,
null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
System.out.println("Database does't exist yet.");
}
if (checkDB != null) {
checkDB.close(); }
return checkDB != null ? true : false;
}
@Override
public synchronized void close() {
if (myDataBase != null) myDataBase.close();
super.close();
} @Override
public void onCreate(SQLiteDatabase arg0) {
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
这是我的 SQLiteConnector 类:
public class SQLiteConnector {
private SQLiteDatabase database;
private SQLiteHelper sqlHelper;
private Cursor cursor;
private static final String TABLE_RECORD = "datasaya";
public SQLiteConnector(Context context) {
sqlHelper = new SQLiteHelper(context);
} // Getting All records
public List<String> getAllRecord() {
List<String> namagambar = new ArrayList<String>();
String selectQuery = "SELECT * FROM " + TABLE_RECORD;
database = sqlHelper.getReadableDatabase();
cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
namagambar.add(cursor.getString(1));
} while (cursor.moveToNext());
}
database.close();
return namagambar;
}
}
如何解决这个问题?
最佳答案
删除数据库名称的点。
来自: cekdatabase.sqlite
到: cekdatabase
。
关于android - 将 Android 应用程序与 SQLite 数据库连接时找不到表名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22961948/