gpt4 book ai didi

android - 使用带有 SQLiteOpenHelper 的外部 sqlite 数据库没有这样的表错误

转载 作者:行者123 更新时间:2023-11-30 01:55:03 25 4
gpt4 key购买 nike

在我尽可能地浏览了所有资源和论坛两天之后,我仍然没有设法解决我的问题。问题是我在运行我的应用程序时没有收到这样的表错误。这个想法很简单,应用程序应该读取一列中的所有值,将它们放入字符串数组并在微调器中使用该字符串数组。我什至尝试过完全重新安装该应用程序并检查了所有解决方案,但没有一个对我有用。

这是 SQLiteOpenHelper 类的代码:

public class DBConnect extends SQLiteOpenHelper {

private static final String DB_PATH = "/data/data/mypackage.name/databases/";
private static final String DB_NAME = "nutrition_table.sql";
private SQLiteDatabase myDatabase;
private final Context myContext;

public DBConnect(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}

public void createDatabase() throws IOException{

boolean dbExist = checkDataBase();

if(dbExist){

}else{

this.getReadableDatabase();

try {

copyDataBase();

} catch (IOException e) {

throw new Error("Error copying database");
}
}
}

private boolean checkDataBase(){

File dbFile = myContext.getDatabasePath(DB_NAME);
return dbFile.exists();
}

private void copyDataBase() throws IOException{

//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);

// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;

//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);

//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}

//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();

}

public void openDataBase() throws SQLException{

//Open the database
String myPath = DB_PATH + DB_NAME;
myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}

@Override
public synchronized void close() {

if(myDatabase != null)
myDatabase.close();

super.close();
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}

这是我的数据库 nutrition_table.sql,位于使用 SqliteBrowser 创建和导出的 Assets 文件夹中。我什至尝试使用自动增量添加 _id 主键,但这也没有用。

BEGIN TRANSACTION;
CREATE TABLE `food` (
`name` TEXT,
`numbers` INTEGER
);

INSERT INTO `food` VALUES ('egg',4);
INSERT INTO `food` VALUES ('bread',7);
INSERT INTO `food` VALUES ('milk',10);
CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US');
INSERT INTO `android_metadata` VALUES ('en_US');
COMMIT;

Activity 类:

public class HomeActivity extends AppCompatActivity {

DBConnect dbConnect;
public String[] names;
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_layout);

//database
dbConnect = new DBConnect(this.getApplicationContext());
dbConnect = new DBConnect(this);
try{
dbConnect.createDatabase();

} catch (IOException e) {
e.printStackTrace();
}
dbConnect.openDataBase();

SQLiteDatabase db = dbConnect.getReadableDatabase();

int columnIndex = 0; // Whichever column your float is in.
Cursor cursor = db.rawQuery("SELECT name FROM food", null);
names = new String[cursor.getCount()];

if (cursor.moveToFirst())
{
for (int i = 0; i < cursor.getCount(); i++)
{
names[i] = cursor.getString(columnIndex);
cursor.moveToNext();
}
}
cursor.close();
db.close();

spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item, names);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);

我也有外部存储的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

最后报错:

09-03 15:31:47.984    3360-3360/? E/SQLiteLog﹕ (1) no such table: food
09-03 15:31:47.994 3360-3360/? D/AndroidRuntime﹕ Shutting down VM
09-03 15:31:47.994 3360-3360/? W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40a122a0)
09-03 15:31:48.014 3360-3360/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{nlg.com.hl/nlg.com.hl.HomeActivity}: android.database.sqlite.SQLiteException: no such table: food (code 1): , while compiling: SELECT name FROM food

感谢您的帮助!

最佳答案

在 SQLiteOpenHelper 的 OnCreate 中你需要创建数据库。在SQLiteOpenHelper的OnCreate中写数据库创建步骤

为什么表名要用引号引起来?

您还使用了太多复杂的代码查看此示例以获得更多帮助:- http://www.javatpoint.com/android-sqlite-tutorial

关于android - 使用带有 SQLiteOpenHelper 的外部 sqlite 数据库没有这样的表错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32380876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com