无法从SQLite数据库中提取数据

时间:2018-03-07 15:56:03

标签: java android sqlite android-sqlite

我是Java新手并试图为我和我的朋友玩的视频游戏构建一个配套应用程序(第一个应用程序!)。有了这个应用程序,我已经构建了一个包含多个表的SQLite数据库,并尝试调用它,查询它,并将这些数据显示在表中。

我的问题: 已编辑

  

我创建了一个外部SQLite数据库并将其复制到我的/ assets / databases /文件夹中。在研究我发现大多数人都说你必须将它复制到/ data / data / Package / databases位置才能真正使用它。

     

尝试访问我的数据时,我收到的表格不存在"错误如果我告诉它抓取数据。如果我删除了调用我的数据的部分,应用程序加载正常并移动到活动正常,但如果我无法提取数据则无用。


E/AndroidRuntime: FATAL EXCEPTION: main
Process: info.ndakgamers.monsterdatabasetrial, PID: 17209
java.lang.RuntimeException: Unable to start activity ComponentInfo{info.ndakgamers.monsterdatabasetrial/info.ndakgamers.monsterdatabasetrial.Anjanath}: android.database.sqlite.SQLiteException: no such table: lrgmonsters (code 1): , while compiling: SELECT monstername, element, ailments, weakness, resistances, locations FROM lrgmonsters WHERE MonsterName = 'Anjanath'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Caused by: android.database.sqlite.SQLiteException: no such table: lrgmonsters (code 1): , while compiling: SELECT monstername, element, ailments, weakness, resistances, locations FROM lrgmonsters WHERE MonsterName = 'Anjanath'
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)


现在,在任何人提起之前,我已经在互联网上搜索过了。我已经阅读了很多博客和主题等等,而且我想知道我需要通过多少工作才能让SQLite数据库在应用程序中运行。

我已经准备好了:"Using your own SQLite database in Android applications"

这就是我用来复制'我的SQLite数据库在内部,所以我可以在应用程序中使用它来查询我的数据。


以下是我的项目:

TestDataHelper.java

package info.ndakgamers.monsterdatabasetrial;

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;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class TestDataHelper extends SQLiteOpenHelper {

//Setting our SQL KEYS
public static final String KEY_ID = "_id";
public static final String KEY_MONSTERNAME = "monstername";
public static final String KEY_ELEMENT = "element";
public static final String KEY_AILMENTS = "ailments";
public static final String KEY_WEAKNESS = "weakness";
public static final String KEY_RESISTANCES = "resistances";
public static final String KEY_LOCATIONS = "locations";

//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/info.ndakgamers.monsterdatabasetrial/databases/";
private static String DB_NAME = "MHWResearchData.db";
private static String DB_TABLE_NAME = "large_monsters";
private SQLiteDatabase myDataBase;
private final Context myContext;
private TestDataHelper testhelper;

/**
 * Constructor
 * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
 *
 * @param context
 */

public String gettestData(){
    String[] columns = new String[]{KEY_ID, KEY_MONSTERNAME, KEY_ELEMENT, KEY_AILMENTS, KEY_WEAKNESS, KEY_RESISTANCES, KEY_LOCATIONS};
    //Cursor cursor = myDataBase.rawQuery("SELECT * FROM large_monsters WHERE monstername = 'Anjanath'",null);
    Cursor cursor = myDataBase.query(DB_TABLE_NAME, columns, KEY_MONSTERNAME, null, null, null, null);
    String result = "";

    int iMonsterName = cursor.getColumnIndex(KEY_MONSTERNAME);
    int iElement = cursor.getColumnIndex(KEY_ELEMENT);
    int iAilments = cursor.getColumnIndex(KEY_AILMENTS);
    int iWeakness = cursor.getColumnIndex(KEY_WEAKNESS);
    int iResistances = cursor.getColumnIndex(KEY_RESISTANCES);
    int iLocations = cursor.getColumnIndex(KEY_LOCATIONS);

    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
        result = result + cursor.getString(iMonsterName) + "  "
                + cursor.getString(iElement) + "  "
                + cursor.getString(iAilments) + "  "
                + cursor.getString(iWeakness) + "  "
                + cursor.getString(iResistances) + "  "
                + cursor.getString(iLocations) + "\n";
    }
    return result;
}

public TestDataHelper(Context context) {
    super(context, DB_NAME, null, 2);
    this.myContext = context;
}

/**
 * Check if the database already exist to avoid re-copying the file each time you open the application.
 *
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase() {

    SQLiteDatabase checkDB = null;

    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    } catch (SQLiteException e) {

        //database does't exist yet.

    }
    if (checkDB != null) {
        checkDB.close();
    }
    return checkDB != null;
}

/**
 * Copies your database from your local assets-folder to the just created empty database in the
 * system folder, from where it can be accessed and handled.
 * This is done by transfering bytestream.
 */
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();

}

/**
 * Creates a empty database on the system and rewrites it with your own database.
 */

public void createDatabase() throws IOException {

    boolean dbExist = checkDataBase();

    if (dbExist) {
        //do nothing - database already exist
    } else {

        //By calling this method and empty database will be created into the default system path
        //of your application so we are gonna be able to overwrite that database with our database.
        this.getReadableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

}

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

}

// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
// to you to create adapters for your views.

}


Anjanath.java 我的数据抓取位置

package info.ndakgamers.monsterdatabasetrial;

import android.database.SQLException;
import android.database.sqlite.SQLiteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.TextView;

import java.io.IOException;

public class Anjanath extends AppCompatActivity {

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

    TestDataHelper myDbHelper = new TestDataHelper(null);
    myDbHelper = new TestDataHelper(this);

    try {
        myDbHelper.createDatabase();
    } catch (IOException ioe) {
        throw new Error("Unable to create database");

    }
    try {
        TextView tvv = (TextView) findViewById(R.id.tvSQLInfo);
        myDbHelper.openDataBase();
        String newData = myDbHelper.gettestData();
        myDbHelper.close();
        tvv.setText(newData);

    }catch(SQLiteException sqle){
        throw sqle;
    }
 }
}


MainActivity.java

package info.ndakgamers.monsterdatabasetrial;


import android.app.Dialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button anjabtn;


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

    anjabtn = (Button) findViewById(R.id.anjabtn);
    anjabtn.setOnClickListener(this);



}

@Override
public void onClick(View myview) {
    switch (myview.getId()) {
        case R.id.anjabtn:

            boolean testItem = true;
            try {
                Intent anjIntent = new Intent("info.ndakgamers.monsterdatabasetrial.ANJANATH");
                startActivity(anjIntent);

            }catch(Exception excep) {
                testItem = false;
            }finally {
                if (testItem) {
                    Dialog dworks = new Dialog(this);
                    TextView tv = new TextView(this);
                    tv.setText("Success");
                    dworks.setContentView(tv);
                    dworks.show();
                }else {
                    Dialog dfails = new Dialog(this);
                    TextView tv = new TextView(this);
                    tv.setText("Failure");
                    dfails.setContentView(tv);
                    dfails.show();
                }

            }

            break;
    }


 }
}


Anjanath XML,如果有人好奇并希望帮助我将每个SQLite列排序到每列

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context="info.ndakgamers.monsterdatabasetrial.Anjanath">

    <ImageView
        android:id="@+id/anjaBanner"
        android:layout_width="320dp"
        android:layout_height="144dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@drawable/anjanath_render" />

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/tblMonsterNames"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="Monster Names"
                    android:textAlignment="center" />

                <TextView
                    android:id="@+id/tblElement"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="Element"
                    android:textAlignment="center" />

                <TextView
                    android:id="@+id/tblAilments"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="Ailments"
                    android:textAlignment="center" />

                <TextView
                    android:id="@+id/tblWeakness"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="Weakness"
                    android:textAlignment="center" />

                <TextView
                    android:id="@+id/tblResistances"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="Resistances"
                    android:textAlignment="center" />

                <TextView
                    android:id="@+id/tblLocations"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="Locations"
                    android:textAlignment="center" />


            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <TextView
                    android:id="@+id/tvSQLInfo"
                    android:layout_width="361dp"
                    android:layout_height="61dp"
                    android:text="db Data Goes Here"
                    tools:layout_editor_absoluteX="12dp"
                    tools:layout_editor_absoluteY="163dp" />
            </TableRow>

        </TableLayout>
    </HorizontalScrollView>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

你的助手的onCreate是空的!这意味着您不会创建任何表。您应该为放入数据库的每个tabel创建一个带CREATE TABLE语句的查询,并在helper的onCreate中执行所有这些查询。

另外,既然您已经提到了编写所有这些代码所需的辛勤工作,我建议您查看Room Persistence Library,它会对您有所帮助!

答案 1 :(得分:0)

我认为您的问题是您已将文件复制到 / assets / databases / 但只是使用数据库名称,而您需要在打开时包含数据库子目录文件。而不是

InputStream myInput = myContext.getAssets().open(DB_NAME);

你需要使用: -

InputStream myInput = myContext.getAssets().open("databases/" + DB_NAME);

或者将文件复制到assets文件夹而不是数据库子目录。