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

时间:2015-09-03 16:22:54

标签: android sqlite

经过两天的时间我可能会查看每个资源和论坛,我仍然无法解决我的问题。问题是我在运行我的应用程序时没有遇到这样的表错误。这个想法很简单,应用程序应该读取一列中的所有值,将它们放入字符串数组中并在微调器中使用该字符串数组。我甚至尝试完全重新安装应用程序并查看了所有解决方案,但它们都没有为我工作。

以下是SQLiteOpenHelper类的代码:

if(!($e.target).hasClass(".btn-asp")){
    if (!($(e.target).is('input[type="radio"]') || $(e.target).is('input[type="checkbox"]'))) e.preventDefault() // here's the issue
}

这是位于使用SqliteBrowser创建和导出的assets文件夹中的我的数据库nutrition nutrition营地。我甚至尝试使用自动增量添加_id主键,但这也没有用。

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) {

    }
}

活动类:

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;

我还拥有外部存储的权限:

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"/>

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

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

为什么表名周围有引号?

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