使用导入和填充的Android DB

时间:2018-02-06 17:56:41

标签: android sqlite

我需要使用已在我的项目中填充的内部数据库,我已经搜索了互联网并尝试了两种方式,它们都没有开始工作,第二种现在出现了这个错误:

no such table: test (code 1):, while compiling: SELECT * FROM test

我需要解决这个问题,有人知道解决这个问题的方法,我需要一个内部的bd已经填充,有人知道某些api或者如何解决它?我正在开发最新版本的android *。

数据库类

public class DatabaseOpenHelper extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "teste.db";
private static final int DATABASE_VERSION = +1;

public DatabaseOpenHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
 }
}

DatabaseAccess类

public class DatabaseAccess {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase database;
private static DatabaseAccess instance;


private DatabaseAccess(Context context) {
    this.openHelper = new DatabaseOpenHelper(context);
}

/**
 * Return a singleton instance of DatabaseAccess.
 *
 * @param context the Context
 * @return the instance of DabaseAccess
 */
public static DatabaseAccess getInstance(Context context) {
    if (instance == null) {
        instance = new DatabaseAccess(context);
    }
    return instance;
}

/**
 * Open the database connection.
 */
public void open() {
    this.database = openHelper.getWritableDatabase();
}

/**
 * Close the database connection.
 */
public void close() {
    if (database != null) {
        this.database.close();
    }
}


public List<String> getQuotes() {
    List<String> list = new ArrayList<>();
    Cursor cursor = database.rawQuery("SELECT * FROM teste", null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        list.add(cursor.getString(1));
        cursor.moveToNext();
    }
    cursor.close();
    return list;
 }
}

MainActivity类

public class MainActivity extends AppCompatActivity {
private ListView lv;
private ListaAdapter adapter;
private List<Produtos> lista;
//private DataBaseHelper dataBaseHelper;
private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.listView = (ListView) findViewById(R.id.lv);
    DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
    databaseAccess.open();
    List<String> quotes = databaseAccess.getQuotes();
    databaseAccess.close();

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, quotes);
    listView.setAdapter(adapter);
 }
}

1 个答案:

答案 0 :(得分:1)

错误显示您有一个数据库,但该数据库中没有名为 test 的表。

没有teste.db文件然后你会得到一个例外,例如: -

Caused by: com.readystatesoftware.sqliteasset.SQLiteAssetHelper$SQLiteAssetException: Missing databases/teste.db file (or .zip, .gz archive) in assets, or target folder not writable

因此,您需要检查 teste.db 是否确实包含名为 test 的表格(也许您称之为表格teste )。可能是您在添加表格后没有保存更改。

但请注意,您显示的消息与您调用表 teste 的代码中的代码无关,具体如下: -

Cursor cursor = database.rawQuery("SELECT * FROM teste", null);

我已经检查并运行了您的代码,一旦您纠正了表名问题(即使用数据库中表的名称(如果这是问题则添加它)),您的代码可以工作并填充和显示ListView。

此链接显示了一些可能有用的实用程序(logDatabaseInfo可能很有用)Are there any methods that assist with resolving common SQLite issues? logDatabaseInfo 包含表的列表。

实施例

将以下方法添加到DatabaseAccess.java

    public void logDBInfo() {
        CommonSQLiteUtilities.logDatabaseInfo(database);
    }

然后将MainActivity.java更改为(请参阅//&lt;&lt;&lt;&lt;&lt;所做的更改): -

public class MainActivity extends AppCompatActivity {
    private ListView lv;
    //private ListaAdapter adapter; //<<<< Commented out
    //private List<Produtos> lista; //<<<< Commented out
    //private DataBaseHelper dataBaseHelper;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.listView = (ListView) findViewById(R.id.lv);
        DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
        databaseAccess.open();
        databaseAccess.logDBInfo(); //<<<< Added
        List<String> quotes = databaseAccess.getQuotes();
        databaseAccess.close();

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, quotes);
        listView.setAdapter(adapter);
    }
}

示例输出: -

02-06 20:48:28.145 1715-1715/? W/SQLiteAssetHelper: copying database from assets...
02-06 20:48:28.145 1715-1715/? W/SQLiteAssetHelper: database copy complete
02-06 20:48:28.185 1715-1715/? I/SQLiteAssetHelper: successfully opened database teste.db
02-06 20:48:28.185 1715-1715/? D/SQLITE_CSU: DatabaseList Row 1 Name=main File=/data/data/examples.mjt.sqliteassethelperexample/databases/teste.db
02-06 20:48:28.185 1715-1715/? D/SQLITE_CSU: Database Version = 1
02-06 20:48:28.189 1715-1715/? D/SQLITE_CSU: Table Name = test Created Using = CREATE TABLE `test` (
                                                `_id`   INTEGER,
                                                `col1`  TEXT,
                                                `col2`  TEXT,
                                                PRIMARY KEY(`_id`)
                                             )
02-06 20:48:28.189 1715-1715/? D/SQLITE_CSU: Table = test ColumnName = _id ColumnType = INTEGER Default Value = null PRIMARY KEY SEQUENCE = 1
02-06 20:48:28.189 1715-1715/? D/SQLITE_CSU: Table = test ColumnName = col1 ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
02-06 20:48:28.189 1715-1715/? D/SQLITE_CSU: Table = test ColumnName = col2 ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0
02-06 20:48:28.189 1715-1715/? D/SQLITE_CSU: Table Name = android_metadata Created Using = CREATE TABLE android_metadata (locale TEXT)
02-06 20:48:28.189 1715-1715/? D/SQLITE_CSU: Table = android_metadata ColumnName = locale ColumnType = TEXT Default Value = null PRIMARY KEY SEQUENCE = 0

注释

  • 已包含从SQLiteAssetHelper发出的消息(首次运行,后续运行,您只需获取最后一个successfully opened database teste.db
  • 显然这是我创建的数据库