Rakesh L
Rakesh L

Reputation: 1178

Stuck with database Insertion

I'm developing an android application where I'm Inserting values from the Server into the local database. Everytime I insert I'll check whether the same row already exists or not using some value. If already exists means I'll do the update .otherwise, Insertion.

Like the following

if(checkValueAlreadyInDb("SELECT * FROM User WHERE UserID='"+fieldValues
                                .getProperty("UserID").toString()+"'"))
                        {
                            updateDb("User", contentValues," UserID='"+fieldValues
                                .getProperty("UserID").toString()+"'" );

                        }
                        else
                        { 
                            insertIntoDb("User", contentValues);
                        }

updateDb() function

public void updateDb(String TableName, ContentValues contentValues,
            String whereClause) {
        SQLiteDatabase sqLiteDatabase = db.getWritableDatabase();
        sqLiteDatabase.update(TableName, contentValues, whereClause, null);
        Log.e("Value",db.getReadableDatabase().rawQuery("SELECT * FROM User WHERE"+whereClause, null).getString(2));
        sqLiteDatabase.close();
    }

insertIntoDb() function

public void insertIntoDb(String TableName, ContentValues contentValues) {
        SQLiteDatabase sqLiteDatabase = db.getWritableDatabase();
        sqLiteDatabase.insert(TableName, null, contentValues);
        sqLiteDatabase.close();
    }

In my Database the table User is created like below,

DATABASE_TABLES.add("CREATE TABLE User("
              +"UserID INTEGER  NOT NULL  ,"
              +"UserName varchar(50) NULL,"
                +"Password varchar(50) NULL,"
                +"FirstName varchar(50) NULL ,"
                +"LastName varchar(50) NULL,"
                +"CompanyName varchar(50) NULL,"
                +"MobilePhone LONG NULL,"
                +"EmailAddress varchar(50) NULL,"
                +"Active BOOLEAN NULL,"
                +"ClientID INTEGER NULL,"
                +"RoleID INTEGER NULL,"
                +"ProjectID INTEGER NULL,"
                +"ProductID INTEGER NULL,"
                +"CreatedBy varchar(50) NULL,"
                +"UpdatedBy varchar(50) NULL ,"
                +"Createdtime DATE NULL ,"
                +"UpdatedTime DATE NULL,"
                +"IsDeleted BOOLEAN NULL)");

My problem is after inserting or updating the values , the cursor count of User table is zero if I create cursor like below

 database.getReadableDatabase.rawQuery("SELECT * FROM User where UserName='Rakesh' and Password='123' and Active='true'",null).getString(0);

and throws the exception

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 126

I can't realize where the mistake is happened . please give me a solution .

Upvotes: 0

Views: 105

Answers (2)

Saikat Das
Saikat Das

Reputation: 95

Sqlite does not support 'varchar' data type so make it text.Sql datatypes
And like others pointed,do check whether your return result, that is the cursor ,has results in it with getCount() function.To do that take result of your query into a Cursor object.Later extract the string from the cursor.You are supplying column 0 to the getString() function which is wrong.

Upvotes: 0

M D
M D

Reputation: 47817

try this way:

 Cursor mCursor = database.getReadableDatabase.rawQuery("SELECT * FROM User where UserName='Rakesh' and Password='123' and Active='true'",null);

  if(mCursor.getCount()>0 &&  mCursor.moveToFirst()){

  do{
   Log.d("data: ",mCursor.getString(0));

   }while(mCursor.moveToNext(););

  }

And make sure that you have inserted data into your Table.

Upvotes: 2

Related Questions