值未插入SQLite数据库

时间:2017-12-20 04:52:06

标签: java android sqlite

我使用two edittextsEmailpassword)和2个按钮(submitviewdata)创建了一个用于测试的应用。我正在调试这个应用程序,它工作正常。

我想通过添加一个EdittextName)来更新此应用,并升级SQLite Database方法中的onUpgrade()。代码如下所示。

public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="upgrade";
private static final int DATABASE_VERSION=2;

//details table details
public static final String TABLE_NAME_Details="details";
public static final String USERNAME="USERNAME";
public static final String PASSWORD="PASSWORD";
public static final String Name="Name"; //newly added column in version 2


public static final String TABLE_NAME_Details_temp="temp";
public static final String USERNAME_temp="USERNAME";
public static final String PASSWORD_temp="PASSWORD";
public static final String Name_temp="Name";

//create table statements for version 2
public static final String Create_Table_Details = "CREATE TABLE "
+ TABLE_NAME_Details + " (" + USERNAME + " TEXT PRIMARY KEY,"
+ PASSWORD + " TEXT ,"
+ Name + " TEXT"
+")";

/*   creation for version 1
private static final String Create_Table_Details = "CREATE TABLE " + 
TABLE_NAME_Details + "("
+ USERNAME + " TEXT PRIMARY KEY,"
+ PASSWORD + " TEXT"
+ ")";*/
//Alter table statements FOR onUpgrade()
//private static final String ALTER_Details = "ALTER TABLE " + 
TABLE_NAME_Details + " ADD COLUMN" + Name + " TEXT";
public DatabaseHelper(Context context)
{
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try{
db.execSQL(Create_Table_Details);
Log.d("database","installed successfully");
}catch (Exception e)
{
e.printStackTrace();
Log.e("/test","Exception due to"+e.toString());
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{

String TEMP_CREATE_CONTACTS_TABLE = "CREATE TABLE " + 
TABLE_NAME_Details_temp + "("
+ USERNAME_temp + " TEXT," + PASSWORD_temp + " TEXT )";
db.execSQL(TEMP_CREATE_CONTACTS_TABLE);

db.execSQL("INSERT INTO " + TABLE_NAME_Details_temp + " SELECT " +  
USERNAME + ", "
+  PASSWORD + " FROM " + TABLE_NAME_Details);

db.execSQL("DROP TABLE "+ TABLE_NAME_Details);

String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME_Details + "("
+ USERNAME + " TEXT," + PASSWORD + " TEXT," + Name + " TEXT )";
db.execSQL(CREATE_CONTACTS_TABLE);
db.execSQL("INSERT INTO " + TABLE_NAME_Details + " SELECT " +  
USERNAME_temp + ", "
+  PASSWORD_temp + ", " +  Name_temp + ", " + null + " FROM " + 
TABLE_NAME_Details_temp);
db.execSQL("DROP TABLE " + TABLE_NAME_Details);
}

public boolean insertData(String username, String password,String name)
{
try{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(USERNAME,username);
contentValues.put(PASSWORD,password);
contentValues.put(Name,name);
long result=db.insertOrThrow(TABLE_NAME_Details, null, contentValues);
if(result==-1)
return false;
else
return true;
}catch(Exception e)
{
e.printStackTrace();
Log.e("/test","Exception due to"+e.toString());
return false;
}
}
//update value
public boolean updatePassword(String LoggedUsername)
{
SQLiteDatabase db = null;
try {
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(PASSWORD, "test");
if (db.isOpen()) {
db.update(TABLE_NAME_Details, values, USERNAME + "='" + 
LoggedUsername+"'",null);
return true;
}
else
return false;
} catch (Exception e) {
Log.d("eEmp/DBUpdateUser ", e.toString());
return false;
}
}
//getting data from database
public Cursor getAllData()
{
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME_Details,null);
return res;
}
}

我安装了第一个版本并正常工作然后卸载。

卸载后,安装第二个版本并存储数据。它也工作正常。

当我尝试在调试模式下使用第二个版本更新第一个版本时,应用程序已成功更新,并且第一个版本中输入的数据可见。当我试图将数据存储在第二版时,数据不会插入到sqlite数据库。为什么呢?

调试时,调用insert方法但是if(result==-1)我得-1而不是1.所以,数据控制来自方法,数据没有插入数据库。

例如:在第一个版本中:我输入了用户名:stackoverflow和密码stackoverflow。 我将应用更新到第二版并成功更新。现在我输入了用户名:hello密码:hello名称:hello,然后点击提交按钮。

之后如果我点击viewdata按钮:它显示用户名:stackoverflow密码stackoverflow名称:null。但不显示第二版中插入的数据。

任何帮助将不胜感激。请帮助我找到正确的解决方案。

谢谢。

3 个答案:

答案 0 :(得分:1)

有一个打字错误检查你的列名是db是

public static final String Name="Name"; //newly added column in version 2

并且您正在使用NAME_在sqlite中插入数据时更改它

试试这个

try{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues=new ContentValues();

    contentValues.put(USERNAME,username);
    contentValues.put(PASSWORD,password);
    contentValues.put(Name,name);
    long result=db.insert(TABLE_NAME_Details, null, contentValues);
    if(result==-1)
        return false;
    else
        return true;
}catch(Exception e)
{
    e.printStackTrace();
    Log.e("/test","Exception due to"+e.toString());
    return false;
}

答案 1 :(得分:1)

试试这个..

      // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        String TEMP_CREATE_CONTACTS_TABLE = "CREATE TABLE " + TEMP_TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_ADDRESS + " TEXT)";
        db.execSQL(TEMP_CREATE_CONTACTS_TABLE);

        // Create an temporaty table that can store data of older version

        db.execSQL("INSERT INTO " + TEMP_TABLE_CONTACTS + " SELECT " +  KEY_ID + ", "
                +  KEY_NAME + ", " +  KEY_ADDRESS + " FROM " + TABLE_CONTACTS);

// Insert data into temporary table from existing older version database (that doesn't contains ADDRESS2 //column)

        db.execSQL("DROP TABLE "+ TABLE_CONTACTS);
// Remove older version database table

        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_ADDRESS + " TEXT," + KEY_ADDRESS2 + " TEXT )";
        db.execSQL(CREATE_CONTACTS_TABLE);

// Create new table with ADDRESS2 column
        db.execSQL("INSERT INTO " + TABLE_CONTACTS + " SELECT " +  KEY_ID + ", "
                +  KEY_NAME + ", " +  KEY_ADDRESS + ", " + null + " FROM " + TEMP_TABLE_CONTACTS);
// Insert data ffrom temporary table that doesn't have ADDRESS2 column so left it that column name as null.     
        db.execSQL("DROP TABLE " + TEMP_TABLE_CONTACTS);
    }

答案 2 :(得分:0)

  1. 不要在onCreate()onUpgrade()中捕获异常。如果出现问题,则该方法不能正常返回。通常返回意味着一切都成功,数据库模式可以使用。

  2. 请注意onUpgrade() ALTER TABLE中缺少的空格:

    private static final String ALTER_Details = "ALTER TABLE " + TABLE_NAME_Details + " ADD COLUMN" + NAME_ + " TEXT";
    

    介于COLUMNNAME_之间。这会导致升级的数据库无法正常工作。全新安装仅通过onCreate()onUpgrade()中的错误未公开。

  3. 使用insertOrThrow()代替insert()来获取更多有用的错误消息,例如"没有这样的列"。