2013-12-15 27 views
0

我遇到了一些數據庫活動問題。我無法寫入數據庫並使用以下代碼查看數據庫。SQLiteAssetHelper和getWritableDatabase

我的數據庫活動:

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活動:

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的活動:

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

+1

你的問題是什麼? –

+0

@CL。如何使用SQLiteAssetHelper寫入此數據庫? – user3104217

回答

0

要寫入到數據庫,嘗試的方法是這樣的:

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

問號由字符串數組中的值替換。您的列名不能使用字符串數組插入,因爲execSQL方法在這些值的周圍放置引號。如果您嘗試,查詢將失敗。

有兩種方法可以從數據庫中讀取數據(甚至可能有兩種方法可以寫入數據庫)。我個人比較喜歡使用原始查詢,像這樣一個讓整個數據庫:

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

然後可以使用光標在一個CursorAdapter創建列表。

相關問題