1
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE);
        // providers data
//      db.execSQL("insert  into "
//              + TABLE_PROVIDERS
//              + " (_id, provider_id, parent_id, title, image, sh_n, enable, visible, image_bg, type, is_group, hide_in_search, limit_min, limit_max, off_on_line, invoice_search) "
//              + "VALUES (1,600,101,'Источники оплаты','',1,1,0,'','',1,1,0,0,0,0)");

        ContentValues cv = new ContentValues();
        cv.put("_id", 1);
        cv.put("provider_id", 600);
        cv.put("parent_id", 101);
        cv.put("title", "Bla");
        cv.put("image", "bla");
        cv.put("sh_n", 1);
        cv.put("enable", 1);
        cv.put("visible", 1);
        cv.put("image_bg", "");
        cv.put("type", "");
        cv.put("is_group", 1);
        cv.put("hide_in_search", 1);
        cv.put("limit_min", 10);
        cv.put("limit_max", 10000);
        cv.put("off_on_line", 1);
        cv.put("invoice_search", 1);

        db.insertOrThrow(TABLE_PROVIDERS, null, cv);
}

Now I'm doing as was suggested, inserting row by row:

import java.util.*;

import android.content.*;
import android.database.*;
import android.database.sqlite.*;

public class ProviderDataSource {

    private SQLiteDatabase database;
    private MySQLiteHelper dbHelper;

    public ProviderDataSource(Context context) {
        dbHelper = new MySQLiteHelper(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }

    public List<Provider> getProvidersByParentId(long parentId) {

        List<Provider> providersList = new ArrayList<Provider>();

        Cursor cursor = database.query(MySQLiteHelper.TABLE_PROVIDERS, new String[] { "provider_id", "title", "image" }, " parent_id=" + parentId,
                null, null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Provider provider = cursorToProvider(cursor);
            providersList.add(provider);
            cursor.moveToNext();
        }
        cursor.close();
        System.out.println("getProvidersByParentId");
        return providersList;
    }

    private Provider cursorToProvider(Cursor cursor) {

        Provider provider = new Provider();
        provider.setId(cursor.getInt(1));
        provider.setTitle(cursor.getString(3));
        provider.setImg(cursor.getString(4));

        return provider;

    }

}

It doesn't work. It seems like the row wasn`t inserted and the array returnd by getProvidersByParentId method is empty. Parent_id as argument is 101.

Arthur Kushman
  • 2,809
  • 9
  • 44
  • 64

3 Answers3

4

Your insert statement is wrong. In SQLite, you cant insert multiple records by separating them with comma, in fact you need to prepare separate insert commands for that. But if your SQLite version is 3.7.11 then its possible.

Read this.

Community
  • 1
  • 1
waqaslam
  • 64,866
  • 15
  • 157
  • 170
  • Ok, what if I have more than 1-10 rows to insert, like dump for example with 10000 rows - what should I do in this situation? – Arthur Kushman Apr 10 '12 at 10:16
  • 1
    Begin an SQLite Transaction, use for-loop to add records and finally commit that transaction. It will be real fast – waqaslam Apr 10 '12 at 10:23
0

You can do it with a for loop like this

// add a button click event like

String[] arr1 = new String[n]; String[] arr2 = new String[n]; String[] arr3 = new String[n];

addButton.setOnClickListener
            (
                new View.OnClickListener()
                {
                    @Override public void onClick(View v) {


                               for(int i=1;1<4;i++){


            try
            {
                // ask the database manager to add a row given the two strings

                //db object of database manager class 

                db.addValues
                (
                    arr1[i] , 
                    arr2[i] , 
                    arr3[i] , 

                );


                           }
catch (Exception e)
            {
                Log.e("Add Error", e.toString());
                e.printStackTrace();
            }

                                }

                                     );

// function of insertion

public void addValues(String Col1, String Col2,
                          String col3)

{

ContentValues values = new ContentValues();




        values.put(Columns1, Col1);
        values.put(Columns2, Col2);
        values.put(Columns3, Col3);

try{
            db.insert(Course_Info_Table, null, values);


}
        catch(Exception e)
        {

        }

}
Avi Dhiman
  • 4,905
  • 7
  • 36
  • 69
0

use ContentValues to insert data into table like this.

SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();

        cv.put("_id", 1);
        cv.put("provider_id", 600);
        cv.put("parent_id", 0);

        cv.put("title", "Источники оплаты");
        cv.put("image", "");
        cv.put("sh_n", 1);
        cv.put("enable", 1);
        cv.put("visible", 0);
        cv.put("image_bg", "");
        cv.put("type", "");
        cv.put("is_group", 1);
        cv.put("hide_in_search", 1);
        cv.put("limit_min", 0);
        cv.put("limit_max", 0);
        cv.put("off_on_line", 0);
        cv.put("invoice_search", 0);
        db.insertOrThrow(TABLE_PROVIDERS, null, cv);
          db.close();

for multiple records call this methods multiple times.

public void insertMydata(int _id, int provider_id, int parent_id /*....add more parameters as you required */){

                SQLiteDatabase db = this.getWritableDatabase();
            ContentValues cv = new ContentValues();

            cv.put("_id", _id);
            cv.put("provider_id", provider_id);
            cv.put("parent_id", parent_id);
               /*
                 ......
                 ..... 
                 put more code here for other columns
                 ......
               */
                db.insertOrThrow(TABLE_PROVIDERS, null, cv);
                db.close();


}
Ravi1187342
  • 1,231
  • 7
  • 14