2

Android 2.3.3

I am new to using AsyncTask, so, my implementation may or may not be right. I have to insert data into database using a AsyncTask.

The problem is there are no errors / exceptions, but the database is not being created. I didn't know whether the problem is related to database or asynctask. So I am providing the code below. please have a look.

Database File :::

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class Database_SpeedDial extends SQLiteOpenHelper{

    static String name = "speeddial_contacts";
    static int version = 1;

    SQLiteDatabase db;

    public Database_SpeedDial(Context context) {
        super(context, name, null, version);
        // TODO Auto-generated constructor stub

        db = getWritableDatabase();     
    }

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

        db.execSQL("CREATE TABLE IF NOT EXISTS contacts(count INTEGER, image_url TEXT, name TEXT, number TEXT, bool INTEGER)");     
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

    public void insert_row(int count, String image_url, String name, String number, int bool) {
        // TODO Auto-generated method stub

        db.execSQL("INSERT INTO contacts VALUES('"+ count +"', '"+ image_url +"', '"+ name +"', '"+ number +"', '"+ bool +"')");
    }



}

Here is my java code :::

public class Class_Add_Contact extends Activity implements OnClickListener {

Database_SpeedDial dbObj;

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
        case R.id.btn_addcontact_add:

            System.out.println("Inside onClick");

            // Using AsyncTask
            // alImage, alContactName ,.... are ArrayList<String>
            InsertData id = new InsertData(alImage, alContactName, alPhoneNumber, alBoolean); 
            id.execute();
            System.out.println("Finished Inserting");

            break;

        case R.id.btn_addcontact_cancel:

            System.out.println("Cancel button clicked");

            break;

        default:
            break;
        }

    }


private class InsertData extends AsyncTask<Void, Void, Void>{

        ArrayList<String> alImage_insertdata;
        ArrayList<String> alContactName_insertdata;
        ArrayList<String> alPhoneNumber_insertdata;
        ArrayList<Boolean> alBoolean_insertdata;

        public InsertData(ArrayList<String> alImage, ArrayList<String> alContactName, 
                ArrayList<String> alPhoneNumber, ArrayList<Boolean> alBoolean)
        {
            alImage_insertdata = alImage;
            alContactName_insertdata = alContactName;
            alPhoneNumber_insertdata = alPhoneNumber;
            alBoolean_insertdata = alBoolean;
        }


        @Override
        protected void onPreExecute() { 

            System.out.println("Inside onPreExecute");
        }


        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

            System.out.println("Inside doInBackground");

                    //dbObj = new Database_SpeedDial(this) -> I used this in onCreate()
                    // of the main class but didn't work, so shifted it here...
                    // This doesn't matter i guess...

            dbObj = new Database_SpeedDial(Class_Add_Contact.this);

            for(int i=0; i< alBoolean_insertdata.size();i++)
            {
                if(alBoolean.get(i))
                {
                    dbObj.insert_row(i+1, alImage_insertdata.get(i).toString(), 
                            alContactName_insertdata.get(i).toString(), 
                            alPhoneNumber_insertdata.get(i).toString(), 
                            1);
                }
                else
                {
                    dbObj.insert_row(i+1, alImage_insertdata.get(i).toString(), 
                            alContactName_insertdata.get(i).toString(), 
                            alPhoneNumber_insertdata.get(i).toString(),
                            0);
                }
            }

            return null;
        }   

        @Override
        protected void onPostExecute(Void res) {

            System.out.println("Inside onPostExecute");

        }


    }

}

Image from DDMS :::

enter image description here

The code executes without any problem, but the database is not created. What could be the problem?

Vamsi Challa
  • 11,038
  • 31
  • 99
  • 149
  • 1
    1/ how do you know the database is not created. 2/ why don't you use the insert method rather than the execSQL. 3/ why don't you test the result of your calls ? – njzk2 Apr 09 '13 at 15:45
  • I would have the database in "data" folder, if the database is created. But the data folder is empty. – Vamsi Challa Apr 09 '13 at 15:47
  • Have you check your sql query string ?may be it need some white-space in query string. – Vivek Bajpai Apr 09 '13 at 15:48
  • Did you try inserting and querying for the value? This is a better way to decide if the DB was created or not. – BobTheBuilder Apr 09 '13 at 15:49
  • Have you tried to expand the data folder and looked further into it? The folder will not get updated because it has not been modified; the contents is what has been modified. – Simon Zettervall Apr 09 '13 at 15:49
  • @SimonZettervall Yes i have expanded the data folder and its empty. – Vamsi Challa Apr 09 '13 at 15:50
  • Are you trying this on an emulator? Please try on a real device. – Simon Zettervall Apr 09 '13 at 15:51
  • @SimonZettervall No I am not trying this on the Emulator, I am trying this on Real Device. – Vamsi Challa Apr 09 '13 at 15:53
  • 1
    If so, please use adb shell and go to the data folder. I do not trust eclipse's File Explorer at all. – Simon Zettervall Apr 09 '13 at 15:55
  • @njzk2 Probably you are right... When I tried to get data from table, with if(c.moveToFirst()), it returned true. Eclipse always showed me the values, this is the first time, it hasn't. So, thought, the data was not available. – Vamsi Challa Apr 09 '13 at 16:01
  • @SimonZettervall Your guess was correct. I have values in database, when i retrieved the values. Eclipse fault. – Vamsi Challa Apr 09 '13 at 16:02
  • 1
    Are you sure you are checking for it properly ? http://stackoverflow.com/questions/1105219/android-where-are-database-files-stored/1106563#1106563 – mastDrinkNimbuPani Apr 09 '13 at 16:17

2 Answers2

3

You cannot name a column 'count'. That is a reserved keyword.

edit

ok, apparently it is not. but it should be.

njzk2
  • 38,969
  • 7
  • 69
  • 107
0

The onCreate() method where you have CREATE TABLE statement will be fired after first transaction on your database. Therefore run the insert statement first and then check whether your database is created or not.

Marcin S.
  • 11,161
  • 6
  • 50
  • 63
  • Actually he is already doing that. db.execSQL("INSERT INTO contacts VALUES('"+ count +"', '"+ image_url +"', '"+ name +"', '"+ number +"', '"+ bool +"')");. Did you mean him to use the explicit insert command instead of exec? – Simon Zettervall Apr 09 '13 at 15:54
  • Actually it's correct, I still think that insert statement is not called. I would check if he enters to this for(int i=0; i< alBoolean_insertdata.size();i++) loop and if the actual insert statements get run. – Marcin S. Apr 09 '13 at 16:03