每当我尝试执行第二个表DATABASE_TABLE1时,它都没有显示这样的表错误
当我在第一个表中插入但它显示在第二个表中时,它没有显示错误
package com.example.billpay;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class Process {
public static final String KEY_FNAME="person_fname";
public static final String KEY_LNAME="person_lname";
public static final String KEY_EMAIL="person_email";
public static final String KEY_PHNO="person_pno";
public static final String KEY_UNAME="person_uname";
public static final String KEY_PASS="person_pass";
public static final String KEY_EID="person_electricityid";
public static final String KEY_EBID="person_eid";
public static final String KEY_EMONTH="bill_month";
public static final String KEY_EBREAD="bill_reading";
public static final String KEY_EBAMOUNT="bill_amount";
private static final String DATABASE_NAME="bill";
private static final String DATABASE_TABLE="electric";
private static final String DATABASE_TABLE1="electricbill";
private static final int DATABASE_VERSION=1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_UNAME + " TEXT PRIMARY KEY, " +
KEY_FNAME + " TEXT NOT NULL, " +
KEY_LNAME + " TEXT NOT NULL, " +
KEY_EMAIL + " TEXT NOT NULL, " +
KEY_PHNO + " INTEGER NOT NULL, " +
KEY_PASS + " TEXT NOT NULL, " +
KEY_EID + " TEXT NOT NULL, " +
" FOREIGN KEY ("+KEY_EID+") REFERENCES "+DATABASE_TABLE1+" ("+KEY_EBID+"));"
);
//creation of second table
db.execSQL("CREATE TABLE " + DATABASE_TABLE1 + " (" +
KEY_EBID + " TEXT PRIMARY KEY, " +
KEY_EMONTH + " TEXT NOT NULL, " +
KEY_EBREAD + " TEXT NOT NULL, " +
KEY_EBAMOUNT + " TEXT NOT NULL); "
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE1);
onCreate(db);
}
}
public Process(Context c){
ourContext=c;
}
public Process open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry(String pfname, String plname, String pemail,
String puname, String ppass, String pphno, String peid ) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_FNAME, pfname);
cv.put(KEY_LNAME, plname);
cv.put(KEY_EMAIL, pemail);
cv.put(KEY_UNAME, puname);
cv.put(KEY_PASS, ppass);
cv.put(KEY_PHNO, pphno);
cv.put(KEY_EID, peid);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_FNAME, KEY_LNAME, KEY_EMAIL, KEY_UNAME, KEY_PASS, KEY_PHNO, KEY_EID };
Cursor c=ourDatabase.query(DATABASE_TABLE, columns, null , null , null , null , null);
String result = "";
int gfname=c.getColumnIndex(KEY_FNAME);
int glname=c.getColumnIndex(KEY_LNAME);
int gemail=c.getColumnIndex(KEY_EMAIL);
int guname=c.getColumnIndex(KEY_UNAME);
int gpass=c.getColumnIndex(KEY_PASS);
int gphno=c.getColumnIndex(KEY_PHNO);
int geid=c.getColumnIndex(KEY_EID);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result= result + c.getString(gfname) + " " + c.getString(glname) + " " + c.getString(gemail) + " "
+ c.getString(guname) + " " + c.getString(gpass) + " " + c.getString(gphno) + c.getString(geid) + "\n";
}
return result;
}
public String getpass(String k) {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_FNAME, KEY_LNAME, KEY_EMAIL, KEY_UNAME, KEY_PASS, KEY_PHNO};
Cursor c=ourDatabase.query(DATABASE_TABLE, columns, KEY_UNAME + "='" + k + "'", null, null, null, null);
if (c != null){
c.moveToFirst();
String rpassword = c.getString(4);
return rpassword;
}
return null;
}
//second DATABASE function
public long createeentry(String peid, String pemonth, String peamount,
String pereading) {
// TODO Auto-generated method stub
ContentValues ph = new ContentValues();
ph.put( KEY_EBID, peid);
ph.put( KEY_EMONTH, pemonth);
ph.put( KEY_EBREAD, pereading);
ph.put( KEY_EBAMOUNT, peamount);
return ourDatabase.insert(DATABASE_TABLE1, null, ph);
}
public String getedata() {
// TODO Auto-generated method stub
String col[]= new String[] { KEY_EBID, KEY_EMONTH, KEY_EBREAD, KEY_EBAMOUNT };
Cursor l= ourDatabase.query(DATABASE_TABLE1, col, null, null, null, null, null);
String eresult="";
int peid=l.getColumnIndex(KEY_EBID);
int pemonth=l.getColumnIndex(KEY_EMONTH);
int peread=l.getColumnIndex(KEY_EBREAD);
int pamount=l.getColumnIndex(KEY_EBAMOUNT);
for (l.moveToFirst(); !l.isAfterLast(); l.moveToNext()){
eresult= eresult + l.getString(peid) + " " + l.getString(pemonth) + " " + l.getString(peread) + " "
+ l.getString(pamount) + "\n";
}
return eresult;
}}
我也在使用外键atrribute.Hope我正在使用它coreect 这是即将出现的错误
04-24 14:31:37.864: E/SQLiteLog(16297): (1) no such table: electricbill
04-24 14:32:09.080: D/AndroidRuntime(16438): Shutting down VM
04-24 14:32:09.081: W/dalvikvm(16438): threadid=1: thread exiting with uncaught exception (group=0x40e06908)
答案 0 :(得分:0)
再次尝试运行,这次是在增加数据库版本之后。您可能只是在第一个表中测试了这个,触发了创建。但是,成功启动不会尝试调用onUpgrade(除非数据库版本已更改,否则将在删除表后调用onCreate)。
另一种方法是从模拟器/手机卸载以前的版本,然后重新安装。
答案 1 :(得分:0)
更改数据库版本或卸载您的应用,然后重新安装它肯定会有效