卡住了数据库插入

时间:2014-04-08 06:10:15

标签: java android database sqlite

我正在开发一个Android应用程序,我将服务器中的值插入到本地数据库中。每次我插入I' ll检查同一行是否已存在或使用某个值。如果已经存在意味着我将进行更新。另外,插入。

如下所示

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

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

updateDb()函数

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()函数

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

在我的数据库中,表格User创建如下,

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)");

我的问题是在插入或更新值后,用户表的光标数为零 如果我在下面创建光标

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

并抛出异常

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

我无法意识到错误发生的地方。请给我一个解决方案。

2 个答案:

答案 0 :(得分:2)

尝试这种方式:

 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(););

  }

确保已将数据插入表格。

答案 1 :(得分:0)

Sqlite不支持'varchar'数据类型,因此请将其设为文本。Sql datatypes
和其他人指出的那样,检查你的返回结果,即光标,是否使用getCount()函数产生结果。为此,将查询结果导入Cursor对象。后者从光标中提取字符串。你是将第0列提供给getString()函数是错误的。