值未插入 SQLite 数据库 [英] values are not inserting to SQLite database
问题描述
我用两个编辑文本
(Email
和password
)和2个按钮(submit
和 viewdata
).我正在调试这个应用程序,它运行良好.
I created one app for testing with two edittexts
(Email
and password
) and 2 buttons (submit
and viewdata
). I was debug this application and it is working fine.
我想通过再添加一个 Edittext
(Name
) 并升级 onUpgrade() 中的
方法.代码如下所示.SQLite 数据库
来更新此应用程序
I wanted to update this application by adding one more Edittext
(Name
) and upgrade the SQLite Database
in onUpgrade()
method. The code is shown below.
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;
}
}
我安装了第一个版本并且运行良好,然后卸载了.
I installed the 1st version and working fine and then uninstalled.
卸载后,安装第二个版本并存储数据.它也工作正常.
After uninstallation, installed the 2nd version and it stores the data. it is also working fine.
当我尝试在调试模式下用第二个版本更新第一个版本时,应用程序更新成功并且在第一个版本中输入的数据可见.当我尝试将数据存储在第二个版本中时,数据没有插入到 sqlite 数据库中.为什么?
When I am trying to update the 1st version with the 2nd version in debug mode, the app updated successfully and the data that entered in the 1st version is visible. and when I am trying to store the data in 2nd version, the data is not inserting to sqlite database. Why?
调试时调用了insert方法,但是这里if(result==-1)
我得到的是-1而不是1.所以,数据控制从方法出来,数据不是插入到数据库中.
While debugging, insert method is called but here if(result==-1)
I am getting -1 instead of 1. so, data control comes out from the method and the data is not inserting to database.
例如:在第一个版本中:我输入了用户名:stackoverflow
和密码 stackoverflow
.我将应用程序更新到第二个版本并成功更新.现在我输入用户名:hello
password:hello
name:hello
然后点击提交按钮.
For example: In 1st version: I entered username: stackoverflow
and password stackoverflow
.
and I updated the app to 2nd version and updated successfully. Now I entered username: hello
password:hello
name:hello
and then click on submit button.
之后如果我点击viewdata
按钮:它显示用户名:stackoverflow
密码stackoverflow
名称:null.但不显示在第二个版本中插入的数据.
After that If I click on viewdata
button: it display username: stackoverflow
password stackoverflow
name:null. But not displaying the data inserted in 2nd version.
任何帮助将不胜感激.请帮助我提供正确的解决方案.
Any help would be appreciated. Please help me with the correct solution.
谢谢.
推荐答案
试试这个..
// 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);
}
这篇关于值未插入 SQLite 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!