0

I am writing an app for android. I need to read a txt file and write the data in an SQLite file. I have managed to complete all the code, but the part where it should insert the value into the database is not working. I have given the code below:

try{
    ContentValues values = new ContentValues();
    values.put(KEY_NAME, firstNumber); // Contact Name
    values.put(KEY_PH_NO, strfinal); // Contact Phone
    // Inserting Row
    db.insert(TABLE_CONTACTS, null, values);
} catch(Exception e) {
    e.printStackTrace();
}

This code is not entering the value into the database and the database remains empty after the operation is completed. What is wrong with this? Thanks. EDIT: Ok, here is the full code:

public class DatabaseHandler extends SQLiteOpenHelper {

        // All Static variables
        // Database Version
        private static final int DATABASE_VERSION = 1;

        // Database Name
        private static final String DATABASE_NAME = "feedsmanager.sqlite";

        // Contacts table name
        private static final String TABLE_CONTACTS = "table_to_hold_all_values";

        // Contacts Table Columns names
        private static final String KEY_ID = "id";
        private static final String KEY_NAME = "name";//ctid
        private static final String KEY_PH_NO = "phone_number";//feedname,address;feedname;address etc
        Context ctx;

        public DatabaseHandler(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            ctx=context;
        }

        // Creating Tables
        @Override
        public void onCreate(SQLiteDatabase db) {

            //Log.d("in onCreate","twitch1");
            String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                    + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                    + KEY_PH_NO + " TEXT);";

            //Log.d("below createcontacts","twitch1");
            try{
            db.execSQL(CREATE_CONTACTS_TABLE);
            }catch(Exception e){Log.d("create went wrong: "+e,"twitch11");}
            //Log.d("below db.execsql","twitch1");
            populate(db);
    }

            void populate(SQLiteDatabase dbs){
            String line="";


            try{
            InputStream is = ctx.getAssets().open("feedtitlesandaddresses.txt");
            InputStreamReader iz=new InputStreamReader(is);
            BufferedReader br = new BufferedReader(iz);
            //SQLiteDatabase dbs = this.getWritableDatabase();
            while((line=br.readLine())!=null) {
                //Log.d("fcked here6","twitch1");
                StringTokenizer stringTokenizer = new StringTokenizer(line, "<");

                String firstNumber="";
                String strfinal="";
                  firstNumber = (String) stringTokenizer.nextElement();
                  **//Calculations to give values to firstNumber and strfinal excluded

                  ContentValues values = new ContentValues();

                  values.put(KEY_NAME, firstNumber); // Contact Name
                  values.put(KEY_PH_NO, strfinal); // Contact Phone
                  // Inserting Row

                  dbs.insert(TABLE_CONTACTS, null, values);}

                dbs.close();}catch(Exception e){Log.d("yeah error is"+e,"twitch12");}
        }}
Ankit Rawat
  • 1,151
  • 2
  • 10
  • 13
  • Do you get an error in your logcat? Please post it. – span Mar 26 '13 at 21:14
  • @span No! I put it in a try catch block, no error! – Ankit Rawat Mar 26 '13 at 21:15
  • @user2213396: show your `try/catch` – jlordo Mar 26 '13 at 21:16
  • Well, do you get an exception then? What does the stack trace say? You should be able to catch a sql exception. – span Mar 26 '13 at 21:17
  • How do you know the database is empty? Maybe the part of the code that reads the database is wrong. If you're using the emulator, actually download the database and use sqlite3 to read it. – dbDev Mar 26 '13 at 21:17
  • @dbDev Yes I downloaded it off the emulator, it's empty. – Ankit Rawat Mar 26 '13 at 21:19
  • @user2213396: I've asked 25 mins ago: Please show your `try/catch` block. – jlordo Mar 26 '13 at 21:41
  • @user2213396: Thank you for finally posting your `try/catch` block. I'm pretty sure this question will help you find your error: [Is it a bad idea to use printStackTrace() in Android Exceptions?](http://stackoverflow.com/questions/3855187/is-it-a-bad-idea-to-use-printstacktrace-in-android-exceptions) Follow the advice you find there, and if you still can't fix your program post your logcat (that will provide necessary information) here. – jlordo Mar 26 '13 at 21:54
  • Post the whole class, not just the `onCreate()` method. – jlordo Mar 27 '13 at 14:32
  • @jlordo Added the whole class in the edit! – Ankit Rawat Mar 27 '13 at 15:03

3 Answers3

0

Try to place this.db.insert(TABLE_CONTACTS, null, values);. Because I had the same problem few months ago and that is the solution.

mare23
  • 91
  • 1
  • 4
0

instead of db.insert() ... use db.insertOrThrow(), probably some constraint error.

Pozzo Apps
  • 1,859
  • 2
  • 22
  • 32
0

What is your db object? Is it SQLiteDatabase db = this.getWriteableDatabase(); ? I have seen that some people uses getReadAbleDatabase() istead of the getWriteableDatabase() method.

Tobias Moe Thorstensen
  • 8,861
  • 16
  • 75
  • 143