我已尝试过此帖子中发布的解决方案: SQL Android Error: no such table
......无济于事。
我只是尝试打开并从我通过命令行创建的预构建SQLite数据库中获取所有行到我的应用程序。
它将文件保存为" names.db"。
我继续将它移动到我的主项目文件中的一个名为" assets" (" C:\ ... \ AndroidStudioProjects \ MyProject的\应用\ SRC \主\资产\ names.db&#34)。
我一直在使用此堆栈解决方案作为模板,或多或少: https://stackoverflow.com/a/9109728/1438611
以下是我的堆栈跟踪抱怨的类的当前代码:
public class MainActivity extends Activity {
private NamesDataSource dataSource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataSource = new NamesDataSource(this);
try {
dataSource.open();
}
catch (SQLException se) {
se.printStackTrace();
}
ListView lv = (ListView) findViewById(R.id.ListView);
List<Names> values = dataSource.getAllNames();
ArrayAdapter<Names> adapter = new ArrayAdapter<Names>(this, android.R.layout.simple_list_item_1, values);
lv.setAdapter(adapter);
}
}
// DBHelper class
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_NAMES = "names";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "name";
private static final String DATABASE_NAME = "names.db";
private static String DB_PATH = "";
private static final int DATABASE_VERSION = 3;
private static final String DATABASE_CREATE = "CREATE TABLE names (_id integer PRIMARY KEY, name text, phone text, email text);";
private final Context mContext;
private SQLiteDatabase mDataBase;
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
this.mContext = context;
}
public void createDataBase() throws IOException {
//If database not exists copy it from the assets
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist)
{
this.getReadableDatabase();
try
{
//Copy the database from assets
copyDataBase();
Log.e("MySQLiteHelper", "createDatabase database created");
}
catch (IOException mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}
}
private boolean checkDataBase() {
File dbFile = new File(DB_PATH + DATABASE_NAME);
//Log.v("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
private void copyDataBase() throws IOException
{
InputStream mInput = mContext.getAssets().open(DATABASE_NAME);
String outFileName = DB_PATH + DATABASE_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
public boolean openDataBase() throws SQLException
{
String mPath = DB_PATH + DATABASE_NAME;
mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
return mDataBase != null;
}
@Override
public void onCreate(SQLiteDatabase database) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor fetchRows() {
String query = "SELECT * FROM " + TABLE_NAMES;
Cursor cursor = mDataBase.rawQuery(query, null);
return cursor;
}
@Override
public synchronized void close()
{
if(mDataBase != null)
mDataBase.close();
super.close();
}
}
// DAO / Adapter Class
public class NamesDataSource {
private SQLiteDatabase db;
private MySQLiteHelper dbHelper;
private String[] allNames = {
MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_NAME
};
public NamesDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
try {
dbHelper.createDataBase();
}
catch (Exception ioe) {
throw new Error("Unable to create database");
}
try {
dbHelper.openDataBase();
}
catch(Exception se){
se.printStackTrace();
}
}
public void close() {
dbHelper.close();
}
public List<Names> getAllNames() {
List<Names> names = new ArrayList<>();
Cursor cursor = dbHelper.fetchRows();
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
Names row = cursorToName(cursor);
names.add(row);
cursor.moveToNext();
}
cursor.close();
return names;
}
private Names cursorToName(Cursor cursor) {
Names name = new Names();
name.setIds(cursor.getLong(0));
name.setName(cursor.getString(1));
name.setPhone(cursor.getString(2));
name.setEmail(cursor.getString(3));
return name;
}
}
最后,错误报告:
android.database.sqlite.SQLiteException: no such table: names (code 1): , while compiling: SELECT * FROM names
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
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:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
at com.example.user.myproject.MySQLiteHelper.fetchRows(MySQLiteHelper.java:96)
at com.example.user.myproject.NamesDataSource.getAllNames(NamesDataSource.java:49)
at com.example.user.myproject.MainActivity.onCreate(MainActivity.java:44)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
非常感谢愿意看一眼的人 欢呼声。
答案 0 :(得分:0)
我没有看到你创建表格?创建它的代码在哪里?
您需要使用DATABASE_CREATE
onCreate()
中的尝试使用代码database.execSQL(DATABASE_CREATE);
并在onUpgrade()
中添加db.execSQL("DROP TABLE IF EXISTS names");
请记住,它会缓存数据库,因此请尝试从头开始重新安装应用程序。
答案 1 :(得分:0)
此路径错误:
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
它将返回
“/数据/数据/ com.example.user.myproject /数据库/数据库/”
但你只想要
“/数据/数据/ com.example.user.myproject /数据库/”
所以将其改为
DB_PATH = context.getApplicationInfo().dataDir;
或者,
DB_PATH = context.getDatabasePath(DATABASE_NAME).getPath();
我正在使用后者。