开发者

Android Multiple insert data to sqlite

开发者 https://www.devze.com 2023-04-07 21:10 出处:网络
I try to insert multiple data to sqlite in android but I can\'t. I want to create and insert like the code bolow, please help me.

I try to insert multiple data to sqlite in android but I can't. I want to create and insert like the code bolow, please help me.

here is my bad code,

package com.pepakbahasajawa.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class PepakDatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "pepakdb";
    private static final int DATABASE_VERSION = 1;
    //Pembuatan Database
    private static final String DATABASE_CREATE_1 = "create table pepak (_id integer primary key autoincrement, "
        + "name text not null);";
    private static final String DATABASE_CREATE_2 = "create table category (_id integer primary key autoincrement, "
        + "id_pepak integer(11) not null, cat_name varchar(255) not null);";
    private static final String DATABASE_CREATE_3 = "create table subcategory (_id integer primary key autoincrement, "
        + "id_category integer(11) not null, subcat_name varchar(255) not null);";
    private static final S开发者_运维技巧tring DATABASE_CREATE_4 = "create table posts (_id integer primary key autoincrement, "
        + "id_subcategory integer(11) not null, post_name_1 varchar(255) not null, post_name_2 varchar(255));";


    public PepakDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

        @Override
    public void onCreate(SQLiteDatabase database) {
        // TODO Auto-generated method stub
        database.execSQL(DATABASE_CREATE_1);
        database.execSQL(DATABASE_CREATE_2);
        database.execSQL(DATABASE_CREATE_3);
        database.execSQL(DATABASE_CREATE_4);

        String sql =  "INSERT INTO pepak (name), category (name), subcategory (id_category)," +
                "(subcat_name), posts (id_subcategory), (post_name_1), (post_name_2)" + 
                "VALUES (Kawruh Rupa), VALUES (1), VALUES (Tetawuhan), VALUES (1), VALUES (Arane Kembang), VALUES (1)," +
                "VALUES (Kembang aren - Arane Dangu), VALUES (Bunga Aren - Disebut Dangu)";
        Log.v("simapn oke", sql);
        database.execSQL(sql);
    }


    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion,
            int newVersion) {
        Log.w(PepakDatabaseHelper.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        database.execSQL("DROP TABLE IF EXISTS pepak");
        database.execSQL("DROP TABLE IF EXISTS category");
        database.execSQL("DROP TABLE IF EXISTS subcategory");
        database.execSQL("DROP TABLE IF EXISTS posts");
            onCreate(database);
    }

}

Please help. Thanks


Your insert SQL is totally wrong. Try to add one entry and do it again if the first one worked.

String sql = "INSERT INTO pepak (name) VALUES (\"SampleText\");";
database.execSQL(sql);

If that works, you can simply create a new insert SQL statement and finally you will have something like that:

String sql = "INSERT INTO pepak (name) VALUES (\"SampleText\");";
database.execSQL(sql);
sql = "INSERT INTO category (name) VALUES (\"CategoryText\");";
database.execSQL(sql);

If you insert more than 20, you should think about a custom transaction handling to speed the inserts up.

// pseudocode
database.startTransaction();
doAllInserts(); // method/loop for all inserts - depending on your needs
database.setTransactionSuccessfull();
database.commit();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号