得到错误,从SQLite数据库检索数据时,错误、数据库、数据、SQLite

2023-09-04 05:40:48 作者:久了就旧

我已使用SQLite数据库浏览器创建的数据库。我一定要遵循这个例如。

I have created database by using SQLite Database browser . I have to follow this example.

数据库的名称是: jokesdatabase.sql ,其中有一个表笑话 我的资产地方这个文件夹是这样的:资产文件夹

Name of the database is : jokesdatabase.sql,which has one table jokes i places this in assets folder like this: assets folder

现在,我的code为:

DatabaseHelper.java 的

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {
private Context mycontext;

private String DB_PATH = "/data/data/a.b.c/databases/";
private static String DB_NAME = "jokesdatabase.sql"; //I have aslo tru .db extenstion
private static String DB_TABLE = "jokes";

public SQLiteDatabase myDataBase;

public DatabaseHelper(Context context) throws IOException {
    super(context, DB_NAME, null, 1);
    this.mycontext = context;
    boolean dbexist = checkdatabase();
    if (dbexist) {
        // System.out.println("Database exists");
        opendatabase();
    } else {
        // System.out.println("Database doesn't exist");
        createdatabase();
    }
}

public void createdatabase() throws IOException {
    boolean dbexist = checkdatabase();
    if (dbexist) {
        System.out.println(" Database exists.");
    } else {
        this.getReadableDatabase();
        try {
            copydatabase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}

private boolean checkdatabase() {
    // SQLiteDatabase checkdb = null;
    boolean checkdb = false;
    try {
        String myPath = DB_PATH + DB_NAME;
        File dbfile = new File(myPath);
        checkdb = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE) != null;
        checkdb = dbfile.exists();
    } catch (SQLiteException e) {
        System.out.println("Database doesn't exist");
    }

    return checkdb;
}

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 byte to inputfile to 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);

}

public synchronized void close() {
    if (myDataBase != null) {
        myDataBase.close();
    }
    super.close();
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

// This will return a cursor containing database records
public Cursor data() {

    Cursor c;
    c = myDataBase.query(DB_TABLE, null, null, null, null, null, null);
    return c;
}

@Override
public void onCreate(SQLiteDatabase arg0) {
    // TODO Auto-generated method stub

}

}

Result.java 的

Result.java

现在 的Andr​​oidManifest.xml 的

Now Androidmanifest.xml

    

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".DatabaseHelper"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Result" android:label="@string/app_name"></activity>
 </application>

最后我得到的logcat以下错误消息。

And finally I get following error message on logcat.

给我,什么是我的code中的错误。我只希望使用SQLite浏览器来创建数据库。请不要通过编码解决方案给更正此问题而已。是的链接。

Give me, what is the error in my code. I have only want to create database using sqlite browser. please dont give the via coding solution.Correct this problem only. which is link.

推荐答案

没有定义 DatabaseHelper 在你的Andr​​oid清单的活动,它的不是的活动

Don't define your DatabaseHelper as activity in your Android manifest, it is not an Activity.

创建和使用你的 DatabaseHelper 活动中,就像任何其他类。

Create and use your DatabaseHelper from within a Activity, just like any other class.

 
精彩推荐
图片推荐