安卓:从SQLite数据库在运行时读 [英] Android: Reading from SQLite Database at runtime
问题描述
因此,我已在我的Android应用程序使用的数据库。我所要做的很简单,从我的数据库只是读取和一个TextView显示的数据,这里是DataBaseHelper类
So I have made a database to use in my Android app. What I am trying to do is simple, just read from my database and display the data in a TextView, here is the DataBaseHelper class
package com.tix.activity;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper{
private static String DB_PATH = "/data/data/com.tix.activity/databases/";
private static String DB_NAME = "tix";
private SQLiteDatabase myDatabase;
private final Context myContext;
public DatabaseHelper (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() {
// TODO Auto-generated method stub
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer,0,length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
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) {
}
public Cursor data(){
Cursor c;
c = myDatabase.query(DB_NAME,null,null,null,null,null,null);
return c;
}
}
这是我MainActivity类
And this is my MainActivity class
package com.tix.activity;
import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Cursor cur;
TextView tv;
Button next, prev;
DatabaseHelper dbh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.tv);
next = (Button)findViewById(R.id.nbtn);
prev = (Button)findViewById(R.id.pbtn);
dbh = new DatabaseHelper(this);
try {
dbh.createDataBase();
} catch (IOException e2) {
e2.printStackTrace();
}
dbh.openDataBase();
cur = dbh.data();
cur.moveToFirst();
tv.setText(cur.getString(1));
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(cur.isLast())
{
cur.moveToFirst();
tv.setText(""+cur.getString(1));
}
else
{
cur.moveToNext();
tv.setText(""+cur.getString(1));
}
}
});
prev.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(cur.isFirst()){
cur.moveToLast();
tv.setText(""+cur.getString(1));
}
else
{
cur.moveToPrevious();
tv.setText(""+cur.getString(1));
}
}
});
}
}
不过,每次我运行模拟器的应用程序,它只是崩溃。但是,如果我把 tv.setText(cur.getString(0)); 代替 tv.setText(cur.getString(1));
But, everytime I run the app in emulator it just crashes. But if I put in tv.setText(cur.getString(0)); instead of tv.setText(cur.getString(1));
然后我得到的TextView显示EN_US(我真的不希望显示,可能是它的语言环境),并在pressing无论是按键的应用程序崩溃的。
then I get textview to display 'en_US' (which I don't really want to display, probably its from locale) and upon pressing either of the buttons the app crashes.
下面是我得到的LogCat中的错误,
Here is the error that I get on LogCat,
07-03 16:37:45.120: E/CursorWindow(1912): Failed to read row 0, column 1 from a CursorWindow which has 1 rows, 1 columns.
07-03 16:37:45.160: D/AndroidRuntime(1912): Shutting down VM
07-03 16:37:45.160: W/dalvikvm(1912): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
07-03 16:37:45.180: E/AndroidRuntime(1912): FATAL EXCEPTION: main
07-03 16:37:45.180: E/AndroidRuntime(1912): java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
07-03 16:37:45.180: E/AndroidRuntime(1912): at android.database.CursorWindow.nativeGetString(Native Method)
07-03 16:37:45.180: E/AndroidRuntime(1912): at android.database.CursorWindow.getString(CursorWindow.java:434)
07-03 16:37:45.180: E/AndroidRuntime(1912): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
07-03 16:37:45.180: E/AndroidRuntime(1912): at com.tix.activity.MainActivity$2.onClick(MainActivity.java:65)
07-03 16:37:45.180: E/AndroidRuntime(1912): at android.view.View.performClick(View.java:4202)
07-03 16:37:45.180: E/AndroidRuntime(1912): at android.view.View$PerformClick.run(View.java:17340)
07-03 16:37:45.180: E/AndroidRuntime(1912): at android.os.Handler.handleCallback(Handler.java:725)
07-03 16:37:45.180: E/AndroidRuntime(1912): at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 16:37:45.180: E/AndroidRuntime(1912): at android.os.Looper.loop(Looper.java:137)
07-03 16:37:45.180: E/AndroidRuntime(1912): at android.app.ActivityThread.main(ActivityThread.java:5039)
07-03 16:37:45.180: E/AndroidRuntime(1912): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 16:37:45.180: E/AndroidRuntime(1912): at java.lang.reflect.Method.invoke(Method.java:511)
07-03 16:37:45.180: E/AndroidRuntime(1912): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-03 16:37:45.180: E/AndroidRuntime(1912): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-03 16:37:45.180: E/AndroidRuntime(1912): at dalvik.system.NativeStart.main(Native Method)
07-03 16:39:01.480: I/Process(1912): Sending signal. PID: 1912 SIG: 9
07-03 16:39:30.201: W/Trace(1966): Unexpected value from nativeGetEnabledTags: 0
07-03 16:39:30.201: W/Trace(1966): Unexpected value from nativeGetEnabledTags: 0
07-03 16:39:30.631: W/Trace(1966): Unexpected value from nativeGetEnabledTags: 0
07-03 16:39:30.631: W/Trace(1966): Unexpected value from nativeGetEnabledTags: 0
07-03 16:39:31.522: E/CursorWindow(1966): Failed to read row 0, column 1 from a CursorWindow which has 1 rows, 1 columns.
07-03 16:39:31.531: D/AndroidRuntime(1966): Shutting down VM
07-03 16:39:31.531: W/dalvikvm(1966): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
07-03 16:39:31.582: E/AndroidRuntime(1966): FATAL EXCEPTION: main
07-03 16:39:31.582: E/AndroidRuntime(1966): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tix.activity/com.tix.activity.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
07-03 16:39:31.582: E/AndroidRuntime(1966): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-03 16:39:31.582: E/AndroidRuntime(1966): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-03 16:39:31.582: E/AndroidRuntime(1966): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-03 16:39:31.582: E/AndroidRuntime(1966): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-03 16:39:31.582: E/AndroidRuntime(1966): at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 16:39:31.582: E/AndroidRuntime(1966): at android.os.Looper.loop(Looper.java:137)
07-03 16:39:31.582: E/AndroidRuntime(1966): at android.app.ActivityThread.main(ActivityThread.java:5039)
07-03 16:39:31.582: E/AndroidRuntime(1966): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 16:39:31.582: E/AndroidRuntime(1966): at java.lang.reflect.Method.invoke(Method.java:511)
07-03 16:39:31.582: E/AndroidRuntime(1966): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-03 16:39:31.582: E/AndroidRuntime(1966): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-03 16:39:31.582: E/AndroidRuntime(1966): at dalvik.system.NativeStart.main(Native Method)
07-03 16:39:31.582: E/AndroidRuntime(1966): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
07-03 16:39:31.582: E/AndroidRuntime(1966): at android.database.CursorWindow.nativeGetString(Native Method)
07-03 16:39:31.582: E/AndroidRuntime(1966): at android.database.CursorWindow.getString(CursorWindow.java:434)
07-03 16:39:31.582: E/AndroidRuntime(1966): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
07-03 16:39:31.582: E/AndroidRuntime(1966): at com.tix.activity.MainActivity.onCreate(MainActivity.java:38)
07-03 16:39:31.582: E/AndroidRuntime(1966): at android.app.Activity.performCreate(Activity.java:5104)
07-03 16:39:31.582: E/AndroidRuntime(1966): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-03 16:39:31.582: E/AndroidRuntime(1966): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-03 16:39:31.582: E/AndroidRuntime(1966): ... 11 more
我的XML如下所示,这个无所谓,反正
My xml is as follows, this doesn't matter, anyway,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/pbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="33dp"
android:layout_marginTop="48dp"
android:text="Back" />
<Button
android:id="@+id/nbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/pbtn"
android:layout_alignBottom="@+id/pbtn"
android:layout_alignParentRight="true"
android:layout_marginRight="25dp"
android:text="Next" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pbtn"
android:layout_centerHorizontal="true"
android:layout_marginTop="141dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
我在使用SQLite数据库浏览器的数据库,但它包含了一个表称为android_metadata与区域(文本)字段的值是'EN_US,另一台是TIX有一次场为_id和其他领域为笑话。这就是一切。
I made the database using SQLite Database Browser, but it contains a table called android_metadata with locale (text) field with value as 'en_US' and the other table is 'tix' with primary field as '_id' and the other field as 'jokes'. That is all.
任何人都可以指出什么是错的?而且我怎么纠正呢?我确实需要这方面的帮助,因为这是我第一次拍摄在数据库:/
Could anyone point out what is wrong? And how do I rectify it? I definitely need help with this, because this is my first shot at database :/
推荐答案
只是把光标变成一个TextView不起作用。您需要首先获取内容。
Just putting a cursor into a TextView does not work. You need to fetch the content first.
您coukd尝试像这样在你的DB类:
you coukd try it like this in your DB class:
public String data(){
Cursor c;
String result;
c = myDatabase.query(DB_NAME,null,null,null,null,null,null);
try {
if (c.moveToFirst()) {
result = cursor.getString(0);
}
result = c.getString(0);
} finally {
c.close();
}
return result;
}
和你的TextView的:
and in your TextView:
tv.setText(dbh.data());
或者类似的东西。
Or something like that
不是最好的方式,但应该
Not the best way but should work
这篇关于安卓:从SQLite数据库在运行时读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!