在onCreate()函数中添加行

时间:2014-09-15 12:47:51

标签: android sqlite

我想通过onCreate()中的addRow()添加行,但似乎它不起作用。当我在DatabaseManager.java外面这样做时,它可以正常工作。我尝试通过不同的功能这样做,并用它来做。前缀但不起作用。我想在创建数据库时只添加一次行。

我的DatabaseManager.java

package com.example.sqllite;

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

/**
 * Class to manage database
 * 
 * @author Karol
 * 
 */
public class DatabaseManager extends SQLiteOpenHelper {

    /**
     * Constructor
     * 
     * @param context
     */
    public DatabaseManager(Context context) {
        super(context, "kontakty.db", null, 1);
    }

    /**
     * Override onCreate function
     * 
     * Creates table with columns: [id][link][name][thumbail]
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table templates("
                + "id integer primary key autoincrement," + "link text,"
                + "name text," + "thumbail text);" + "");

        addRow(
                "link",
                "name",
                "thumbail");
    }

    /**
     * Overrides onUpgrade method
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    /**
     * Add row to database
     * 
     * @param link
     *            URL to template
     * @param name
     *            Name of template
     * @param thumbail
     *            Url to templates thumbail
     */
    public void addRow(String link, String name, String thumbail) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("link", link);
        values.put("name", name);
        values.put("thumbail", thumbail);
        db.insertOrThrow("templates", null, values);
    }

    /**
     * Returns cursor (rows) for all rows in database
     * 
     * @return Cursor which contains all rows
     */
    public Cursor getAll() {
        String[] columns = { "id", "link", "name", "thumbail" };
        SQLiteDatabase db = getReadableDatabase();
        Cursor c = db.query("templates", columns, null, null, null, null, null);
        return c;
    }

}

Hre是我的Logcat:

09-15 08:39:32.780: D/dalvikvm(858): GC_FOR_ALLOC freed 47K, 5% free 2945K/3072K, paused 118ms, total 119ms
09-15 08:39:32.780: I/dalvikvm-heap(858): Grow heap (frag case) to 3.554MB for 635812-byte allocation
09-15 08:39:32.940: D/dalvikvm(858): GC_FOR_ALLOC freed 2K, 4% free 3563K/3696K, paused 151ms, total 152ms
09-15 08:39:33.310: W/Settings(858): Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
09-15 08:39:33.620: D/AndroidRuntime(858): Shutting down VM
09-15 08:39:33.620: W/dalvikvm(858): threadid=1: thread exiting with uncaught exception (group=0xb3ac6ba8)
09-15 08:39:33.690: E/AndroidRuntime(858): FATAL EXCEPTION: main
09-15 08:39:33.690: E/AndroidRuntime(858): Process: com.example.kontaktysqllite, PID: 858
09-15 08:39:33.690: E/AndroidRuntime(858): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kontaktysqllite/com.example.sqllite.MainActivity}: java.lang.IllegalStateException: getDatabase called recursively
09-15 08:39:33.690: E/AndroidRuntime(858):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
09-15 08:39:33.690: E/AndroidRuntime(858):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-15 08:39:33.690: E/AndroidRuntime(858):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-15 08:39:33.690: E/AndroidRuntime(858):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-15 08:39:33.690: E/AndroidRuntime(858):  at android.os.Handler.dispatchMessage(Handler.java:102)
09-15 08:39:33.690: E/AndroidRuntime(858):  at android.os.Looper.loop(Looper.java:136)
09-15 08:39:33.690: E/AndroidRuntime(858):  at android.app.ActivityThread.main(ActivityThread.java:5017)
09-15 08:39:33.690: E/AndroidRuntime(858):  at java.lang.reflect.Method.invokeNative(Native Method)
09-15 08:39:33.690: E/AndroidRuntime(858):  at java.lang.reflect.Method.invoke(Method.java:515)
09-15 08:39:33.690: E/AndroidRuntime(858):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-15 08:39:33.690: E/AndroidRuntime(858):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-15 08:39:33.690: E/AndroidRuntime(858):  at dalvik.system.NativeStart.main(Native Method)
09-15 08:39:33.690: E/AndroidRuntime(858): Caused by: java.lang.IllegalStateException: getDatabase called recursively
09-15 08:39:33.690: E/AndroidRuntime(858):  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:204)
09-15 08:39:33.690: E/AndroidRuntime(858):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
09-15 08:39:33.690: E/AndroidRuntime(858):  at com.example.sqllite.DatabaseManager.addRow(DatabaseManager.java:61)
09-15 08:39:33.690: E/AndroidRuntime(858):  at com.example.sqllite.DatabaseManager.onCreate(DatabaseManager.java:37)
09-15 08:39:33.690: E/AndroidRuntime(858):  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
09-15 08:39:33.690: E/AndroidRuntime(858):  at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
09-15 08:39:33.690: E/AndroidRuntime(858):  at com.example.sqllite.DatabaseManager.getAll(DatabaseManager.java:76)
09-15 08:39:33.690: E/AndroidRuntime(858):  at com.example.sqllite.MainActivity.onCreate(MainActivity.java:70)
09-15 08:39:33.690: E/AndroidRuntime(858):  at android.app.Activity.performCreate(Activity.java:5231)
09-15 08:39:33.690: E/AndroidRuntime(858):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-15 08:39:33.690: E/AndroidRuntime(858):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
09-15 08:39:33.690: E/AndroidRuntime(858):  ... 11 more
09-15 08:39:41.490: I/Process(858): Sending signal. PID: 858 SIG: 9

4 个答案:

答案 0 :(得分:1)

引起:java.lang.IllegalStateException:递归调用getDatabase

试试这个:

private SQLiteDatabase db;

public void addRow(String link, String name, String thumbail) {
        if(db == null)
        {
            db = getWritableDatabase();
        }
        ContentValues values = new ContentValues();
        values.put("link", link);
        values.put("name", name);
        values.put("thumbail", thumbail);
        db.insertOrThrow("templates", null, values);
    }

您可以将db对象从onCreate()作为参数传递给addRow(),然后移除SQLiteDatabase db = getWritableDatabase();

中的addRow()

答案 1 :(得分:1)

db中的onCreate()作为参数传递给addRow(),然后移除getWritableDatabase()

在上一次触发getWritableDatabase()的{​​{1}}或getWritableDatabase()来电时,您无法致电getReadableDatabase()

答案 2 :(得分:1)

这是你的代码:

package com.example.sqllite;

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

/**
* Class to manage database
*
* @author Karol
*
*/
public class DatabaseManager extends SQLiteOpenHelper {

    private SQLiteDatabase db;

    /**
     * Constructor
     *
     * @param context
     */
    public DatabaseManager(Context context) {
            super(context, "kontakty.db", null, 1);
    }

    /**
     * Override onCreate function
     *
     * Creates table with columns: [id][link][name][thumbail]
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
            db.execSQL("create table templates("
                            + "id integer primary key autoincrement," + "link text,"
                            + "name text," + "thumbail text);" + "");

            // Avoiding Caused by: java.lang.IllegalStateException: getDatabase called recursively
            setDbOnCreate(db);

            addRow(
                            "link",
                    "name",
                    "thumbail");
    }

    /**
     * Overrides onUpgrade method
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    /**
     * Add row to database
     *
     * @param link
     *            URL to template
     * @param name
     *            Name of template
     * @param thumbail
     *            Url to templates thumbail
     */
    public void addRow(String link, String name, String thumbail) {
    if(db == null)
    {
        db = getWritableDatabase();
    }
    ContentValues values = new ContentValues();
    values.put("link", link);
    values.put("name", name);
    values.put("thumbail", thumbail);
    db.insertOrThrow("templates", null, values);
    }

    /**
     * Set private variable to parameter
     * @param db
     */
    private void setDbOnCreate(SQLiteDatabase db) {
            this.db = db;
    }

    /**
     * Returns cursor (rows) for all rows in database
     *
     * @return Cursor which contains all rows
     */
    public Cursor getAll() {
            String[] columns = { "id", "link", "name", "thumbail" };
            SQLiteDatabase db = getReadableDatabase();
            Cursor c = db.query("templates", columns, null, null, null, null, null);
            return c;
    }

}

答案 3 :(得分:0)

原因是getWritableDatabase实际上包含这样的代码:

if (version == 0) {
    onCreate(db);
...
}

在您的onCreate中,您正在呼叫getWritableDatabase,呼叫onCreate,此循环将永远不会完成。