我是android的初学者,我在向表中插入数据时遇到问题。问题已被多次询问,但我无法理解问题的根源及其解决方案。
public long insertRegisterData(RegisterData data) throws SQLException {
ContentValues cv = new ContentValues();
cv.put("u_name", data.getU_name());
cv.put("u_address", data.getU_address());
cv.put("tel_office", data.getTel_office());
cv.put("tel_resi", data.getTel_resi());
cv.put("tel_req", data.getTel_req());
cv.put("tel_fax", data.getTel_fax());
cv.put("dateSanad", data.getDateSanad());
cv.put("memBarCounWelfare", data.getMemBarCounWelfare());
cv.put("prof_tax", data.getProf_tax());
cv.put("pan", data.getPan());
cv.put("drivingPolicyNo", data.getDrivingPolicyNo());
cv.put("drivingPolicyDue", data.getDrivingPolicyDue());
cv.put("bankAcc", data.getBankAcc());
cv.put("passport", data.getPassport());
cv.put("blood", data.getBlood());
cv.put("docName", data.getDocName());
cv.put("docPhone", data.getDocPhone());
cv.put("docAddress", data.getDocAddress());
System.out.println("Comntent vlaues " + cv.get("u_name"));
return sqLiteDatabase.insert("Registration1", null, cv);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create table newRecord(prev_date text,CaseName text,caseNo Text,forParty text,againstParty text,stages text)");
db.execSQL("Create table Registration1(u_name text,u_address text,tel_office text,tel_resi text,tel_req text,tel_fax text,dateSanad text,memBarCounWelfare text,prof_tax text,pan text,drivingPolicyNo text,drivingPolicyDue text,bankAcc text,passport text,blood text,docName text,docPhone text,docAddress text)");
}
在printstackTrace中
02-18 02:08:35.362: E/Database(3622): Error inserting docName=H memBarCounWelfare=Download prof_tax=D drivingPolicyNo=X docAddress=sachin tel_req=Dd blood=G tel_resi=D dateSanad=D tel_office= pan=null drivingPolicyDue=Dddsdfdssd dfg d tel_fax=D u_address=s bankAcc=D docPhone=99 passport=D u_name=S
02-18 02:08:35.362: E/Database(3622): android.database.sqlite.SQLiteException: no such table: Registration1: , while compiling: INSERT INTO Registration1(docName, memBarCounWelfare, prof_tax, drivingPolicyNo, docAddress, tel_req, blood, tel_resi, dateSanad, tel_office, pan, drivingPolicyDue, tel_fax, u_address, bankAcc, docPhone, passport, u_name) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
02-18 02:08:35.362: E/Database(3622): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
02-18 02:08:35.362: E/Database(3622): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
02-18 02:08:35.362: E/Database(3622): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
02-18 02:08:35.362: E/Database(3622): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
02-18 02:08:35.362: E/Database(3622): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
02-18 02:08:35.362: E/Database(3622): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149)
02-18 02:08:35.362: E/Database(3622): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1569)
02-18 02:08:35.362: E/Database(3622): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
02-18 02:08:35.362: E/Database(3622): at com.database.SQLiteAdapter.insertRegisterData(SQLiteAdapter.java:116)
02-18 02:08:35.362: E/Database(3622): at com.diary.OneTimePassword$1.onClick(OneTimePassword.java:107)
02-18 02:08:35.362: E/Database(3622): at android.view.View.performClick(View.java:2485)
02-18 02:08:35.362: E/Database(3622): at android.view.View$PerformClick.run(View.java:9080)
02-18 02:08:35.362: E/Database(3622): at android.os.Handler.handleCallback(Handler.java:587)
02-18 02:08:35.362: E/Database(3622): at android.os.Handler.dispatchMessage(Handler.java:92)
02-18 02:08:35.362: E/Database(3622): at android.os.Looper.loop(Looper.java:123)
02-18 02:08:35.362: E/Database(3622): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-18 02:08:35.362: E/Database(3622): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 02:08:35.362: E/Database(3622): at java.lang.reflect.Method.invoke(Method.java:507)
02-18 02:08:35.362: E/Database(3622): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-18 02:08:35.362: E/Database(3622): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-18 02:08:35.362: E/Database(3622): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
错误的根本原因可以在这里找到:
android.database.sqlite.SQLiteException: **no such table: Registration1**: , while compiling: INSERT INTO Registration1(docName, memBarCounWelfare, prof_tax, drivingPolicyNo, docAddress, tel_req, blood, tel_resi, dateSanad, tel_office, pan, drivingPolicyDue, tel_fax, u_address, bankAcc, docPhone, passport, u_name) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
CREATE TABLE
Registration1
语句失败或未被调用。您可以尝试在onCreate()
中设置断点或插入日志语句,并确保您到达那里。您还可以使用execSQL()
围绕try / catch (SQLException e)
来电,看看是否有SQLException
被抛出。
答案 1 :(得分:0)
问题是您的表 Registration1 不存在。如果数据库不存在,则调用方法onCreate
,这意味着您的数据库是在第一次运行程序时创建的,但如果稍后您添加了第二个表 Registration1 ,则该表将不要被创造。
您需要覆盖数据库Helper类中的onUpgrade
(您可以简单地删除表并调用onCreate()),并且下次运行您的程序时,您需要增加版本号。 SQLiteOpenHelper构造函数。
运行程序时,运行时会将您指定的版本与已存储的版本进行比较,如果它们不同,则运行时会调用onUpgrade
。
编辑:
以下是两个示例,第一个示例更改现有表。第二个示例重新创建表(删除所有以前的数据)。
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
if (oldVersion<2)
{
// Example of altering the table
db.execSQL("ALTER TABLE " + myTable + " ADD COLUMN " + newColumn + " int;");
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// Delete and create table
db.execSQL("DROP TABLE " + myTable + " ;");
onCreate(db);
}