SQLiteAssetHelper和getWritableDatabase [英] SQLiteAssetHelper and getWritableDatabase

查看:193
本文介绍了SQLiteAssetHelper和getWritableDatabase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有有一些麻烦与我的数据库活动。我无法写入数据库并使用以下code查看数据库。

I'm having have some trouble with my Database activity. I am unable to write to the database and view the database using the following code.

我的数据库活动:

package com.jacob.eindproject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

import java.sql.*;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class Database extends SQLiteAssetHelper {

    public static final String KEY_PRODUCT = "Product";
    public static final String KEY_EENHEID = "Eenheid";
    public static final String KEY_KCAL = "Kcal";

    private static final String DATABASE_NAME = "Voedsel";
    private static final String DATABASE_TABLE = "Voeding";
    private static final int DATABASE_VERSION = 1;

    private DbHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;

    private static class DbHelper extends SQLiteAssetHelper{

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, context.getExternalFilesDir(null).getAbsolutePath(), null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

    }

        @Override
        public void onUpgrade(SQLiteDatabase Voedsel, int oldVersion, int newVersion) {
            Voedsel.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(Voedsel);
        }

        public void close(Database database) {
            // TODO Auto-generated method stub

        }




    public Database(Context c){
        ourContext = c;
    }

    public Database open() throws SQLException{
        ourHelper = new DbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;        
    }

    public void close() {

    ourHelper.close();
}



    public long createEntry(String product, String kcal, String eenheid) {
        ContentValues cv = new ContentValues();
        cv.put(KEY_PRODUCT, product);
        cv.put(KEY_EENHEID, eenheid);
        cv.put(KEY_KCAL, kcal);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);

    }

    public String getData() {
        // TODO Auto-generated method stub
        String[] columns = new String[]{ KEY_PRODUCT, KEY_EENHEID, KEY_KCAL};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
        String result = "";

        int iRow = c.getColumnIndex(KEY_PRODUCT);
        int iName = c.getColumnIndex(KEY_EENHEID);
        int iHotness = c.getColumnIndex(KEY_KCAL);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iHotness) + "\n";


        }

        return result;
    }
}

我SQLView活动:

My SQLView activity:

package com.jacob.eindproject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class SQLView extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sqlview);
        TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
        Database info = new Database(this);
        info.open();
        String data = info.getData();
        info.close();
        tv.setText(data);





    }

}

SQLite的活动:

SQLite activity:

package com.jacob.eindproject;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View.OnClickListener;


public class SQLite extends Activity implements View.OnClickListener {

    Button sqlUpdate, sqlView;
    EditText sqlVoeding, sqlKcal, sqlEenheid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sqllite);
        sqlUpdate = (Button) findViewById(R.id.bSQLUpdate);
        sqlVoeding = (EditText) findViewById(R.id.etSQLVoeding);
        sqlEenheid = (EditText) findViewById(R.id.etSQLEenheid);
        sqlKcal = (EditText) findViewById(R.id.etSQLKcal);

        sqlView = (Button) findViewById(R.id.bSQLopenView);
        sqlView.setOnClickListener((android.view.View.OnClickListener) this);
        sqlUpdate.setOnClickListener((android.view.View.OnClickListener) this);
    }

    public void onClick(View arg0) {
        switch (arg0.getId()) {
        case R.id.bSQLUpdate:


            boolean didItWork = true;
            try{            
            String voeding = sqlVoeding.getText().toString();
            String Kcal = sqlKcal.getText().toString();
            String eenheid = sqlEenheid.getText().toString();

            Database entry = new Database(SQLite.this);
            entry.open();
            entry.createEntry(voeding, Kcal, eenheid);
            entry.close();

            }catch (Exception e ){
                didItWork = false;

            }finally{
                if (didItWork){
                    Dialog d = new Dialog(this);
                    d.setTitle("Heak Yeay");
                    TextView tv = new TextView(this);
                    tv.setText("Succes");
                    d.setContentView(tv);
                    d.show();
            }
        }

            break;
        case R.id.bSQLopenView:
            Intent i = new Intent(this, SQLView.class);
            startActivity(i);


        }
        }

    public void onClick(DialogInterface arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    }

我有我的数据库文件(Voedsel.rar),中的资产/数据库/ Voedsel.rar 的。

推荐答案

要写入到数据库,尝试的方法是这样的:

To write to your database, try a method like this:

public void insertVoeding(String product, String eenheid, String kcal) {
    ourDatabase.execSQL("INSERT INTO Voeding (Product, Eenheid, Kcal) VALUES (?, ?, ?)",
              new String[] {product, eenheid, kcal});
}

问号由字符串数组中的值替换。列名不能使用字符串数组插入,因为execSQL方法将围绕这些值引号。如果你尝试查询将失败。

The question marks are replaced by the values in the string array. Your column names cannot be inserted using the string array, because the execSQL method puts quotation marks around these values. The query will fail if you try.

有两种方法从一个数据库(甚至可能是写入它有两种方式,太)来读取。我使用原始查询,像这样的获取整个数据库亲自preFER:

There are two ways to read from a database (may even be two ways to write to it, too). I personally prefer using raw queries, like this one to get the entire database:

public Cursor getEntireDB() {
    return ourDatabase.rawQuery("SELECT * FROM Voeding");
}

您可以再使用光标在CursorAdapter的创建列表。

You can then use the cursor in a CursorAdapter to create a list.

这篇关于SQLiteAssetHelper和getWritableDatabase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆