我正在使用多个关系数据表开发数据收集应用程序。我已经创建了数据库。在第一个表的情况下,代码运行良好。如果我尝试对第二个表运行相同的查询,则即使该表位于数据库中,该表仍显示该表不存在。
数据库结构
public String getName(String Plant) {
c1 = db.rawQuery("select * from Area where PAd = '" + Plant + "'", new String[]{});
StringBuffer buffer = new StringBuffer();
while (c.moveToNext()) {
String plant = c.getString(0);
buffer.append("" + plant);
}
return buffer.toString();
}
错误日志:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dbtest, PID: 28350
android.database.sqlite.SQLiteException: no such table: Area (code 1): , while compiling: select * from Area where PAd = 'Plant02'
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:890)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:501)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1392)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1331)
at com.example.dbtest.DatabaseAcess.getName(DatabaseAcess.java:46)
at com.example.dbtest.MainActivity$3.onClick(MainActivity.java:86)
at android.view.View.performClick(View.java:6305)
at android.view.View$PerformClick.run(View.java:24840)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6501)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
第二张表的表结构
CREATE TABLE Area (
Name TEXT,
Address TEXT,
PAd TEXT
);
第一个表的表结构
CREATE TABLE Table1 (
Name TEXT,
Address TEXT
);
DatabaseOpenHelper
代码。
public class DatabaseOpenHelper extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "MyExternalDatabase1.db";
public static final int DATABASE_VERSION = 1;
public DatabaseOpenHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
}
DatabaseAccess
代码。
public class DatabaseAcess {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase db;
private static DatabaseAcess instance;
Cursor c = null;
Cursor c1 = null;
private DatabaseAcess(Context context){
this.openHelper = new DatabaseOpenHelper(context);
}
public static DatabaseAcess getInstance(Context context){
if(instance == null){
instance = new DatabaseAcess(context);
}
return instance;
}
public void open(){
this.db = openHelper.getWritableDatabase();
}
public void close(){
if(db!=null){
this.db.close();
}
}
public String getAddress(String name){
c= db.rawQuery("select Address from Table1 where Name = '" + name + "'", new String[]{});
StringBuffer buffer = new StringBuffer();
while (c.moveToNext()) {
String address = c.getString(0);
buffer.append("" + address);
}
return buffer.toString();
}
public String getName(String Plant) {
c1 = db.rawQuery("select Name from Area where PAd = '" + Plant + "'", null);
StringBuffer buffer = new StringBuffer();
while (c.moveToNext()) {
String plant = c.getString(0);
buffer.append(""+plant);
}
return buffer.toString();
}
}
答案 0 :(得分:0)
也许这可以帮助您
public String getName(String Plant) {
Cursor c = db.rawQuery("SELECT * FROM Area WHERE PAd='+Plant+'", null);
StringBuffer buffer = new StringBuffer();
while (c.moveToNext()) {
String plant = c.getString(0);
buffer.append(""+plant);
}
return buffer.toString();
}
答案 1 :(得分:0)
我认为外部数据库是指要从asset
目录中加载的数据库。在这里,您需要首先将数据库复制到应用程序内部存储中,以使您的代码可以访问该数据库。因此,我建议您在首次运行该应用程序时执行以下操作。
public static final String DB_PATH = "/data/data/" + "com.your.package.name" + "/databases/";
private void copyFromAssetsAndCreateDatabase() {
InputStream yourDatabaseFromAsset;
try {
yourDatabaseFromAsset = getApplicationContext().getAssets().open("MyExternalDatabase1");
File dir = new File(DataHelper.DB_PATH);
if (!dir.exists()) dir.mkdir();
File f = new File(DataHelper.DB_PATH + "MyExternalDatabase1" + ".sqlite");
if (!f.exists()) {
f.createNewFile();
}
OutputStream mOutput = new FileOutputStream(f);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = yourDatabaseFromAsset.read(mBuffer)) > 0)
mOutput.write(mBuffer, 0, mLength);
mOutput.flush();
mOutput.close();
mInputEnglish.close();
} catch (Exception e) {
e.printStackTrace();
}
}
现在,当将数据库从外部文件夹复制到数据库通常位于的内部存储中时,它应该可以找到数据库而没有任何错误。我不知道第一个数据库。我认为它是在您的应用程序中使用CREATE TABLE
语句创建的。
希望有帮助。
答案 2 :(得分:0)
一个可能的原因是您第一次只创建了第一个表。之后,为第二个表追加代码。注意数据库已创建,将不会再次调用onCreate()。您应该升级数据库版本并在onUpgrade()中创建第二个表。
我以前犯过同样的错误。