0

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 String 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

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
gabisabobo
  • 3
  • 2
  • 6

1 Answers1

2

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();
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • yes thank you WarrenFaith, yes i want to insert more than 20. please give me more about that code, i mean example about a custom transaction. and me newbie android, i search at http://www.droidnova.com about example custom transaction but i don't lucky there. – gabisabobo Sep 27 '11 at 20:58
  • 1
    You should have searched here, too: Its my highest voted answer: http://stackoverflow.com/questions/3501516/android-sqlite-database-slow-insertion/3501572#3501572 – WarrenFaith Sep 27 '11 at 21:03