在单个Dbhelper类中维护2个数据库表

时间:2014-03-13 06:48:28

标签: java android

在我的应用程序中,我试图维护2个数据库表,但我的第二个表不存储值,但它显示成功的数据是存储。这是我的DbHelper类,我的其他java类和log cat .....

请帮助我.....

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "sri.db";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "login";
private static final String TABLE_CREATE ="CREATE TABLE " + TABLE_NAME + "(" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT,"+ 
"username TEXT NOT NULL, password TEXT NOT NULL);";

 private static final String DB_ADMIN = "INSERT INTO "+TABLE_NAME+"values(1, admin,        password);";

public static final String TABLE_NAME_SAVE="saveinfo";
public static final String KEY_ID="_id";
public static final String KEY_URL="surl";
public static final String KEY_UID="suid";
public static final String KEY_PASS="spassword";

public static final String SCRIPT="CREATE TABLE" + TABLE_NAME_SAVE + "(" + "_id     INTEGER PRIMARY KEY AUTOINCREMENT,"+
"surl TEXT NOT NULL, suid TEXT NOT NULL, spassword TEXT NOT NULL);";



public DbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    System.out.println("In constructor");
}

@Override
public void onCreate(SQLiteDatabase db) {
    try{
        //Create Database
        db.execSQL(TABLE_CREATE);
        //create admin account
        db.execSQL(DB_ADMIN);
        //System.out.println("In onCreate");
        db.execSQL(SCRIPT);
    }catch(Exception e){
    e.printStackTrace();
    }



}


    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    onCreate(db);

}
    SQLiteDatabase db;

    public Cursor getAllValues() {
// TODO Auto-generated method stub
 SQLiteDatabase db = this.getWritableDatabase();
String[] clumns={KEY_ID,KEY_URL,KEY_UID,KEY_PASS};
return db.query(TABLE_NAME_SAVE, clumns, null, null, null, null, null);
    }

    void deleteAllRecords(){
  db.delete(TABLE_NAME_SAVE, null, null);
 }

 void deleteOneRecord(String rowid){
  db.delete(TABLE_NAME_SAVE, rowid +"="+KEY_ID, null);
 }

public void openDatabase() {
    // TODO Auto-generated method stub

}



    }

这是我的其他java类userPageActivity.java类

    public class UserpageActivity extends Activity implements OnClickListener {
Button save,reset,cancle,history;
EditText url,userid,password;
 DbHelper mHelper;
    SQLiteDatabase dataBase;
private String murl,muserid,mpass;
//private boolean isUpdate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_userpage);

    save=(Button)findViewById(R.id.bsave);
    reset=(Button)findViewById(R.id.breset);
    cancle=(Button)findViewById(R.id.bcancle);
    history=(Button)findViewById(R.id.viewsi);

    save.setOnClickListener(this);
    reset.setOnClickListener(this);
    cancle.setOnClickListener(this);
    history.setOnClickListener(this);


    url=(EditText)findViewById(R.id.urled);
    userid=(EditText)findViewById(R.id.userided);
    password=(EditText)findViewById(R.id.passworded);

     mHelper=new DbHelper(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.bsave:
    murl=url.getText().toString();
      muserid=userid.getText().toString();
      mpass=password.getText().toString();
        if(murl.length()>0 && muserid.length()>0 && password.length()>0)
        {
            saveData();
        }
        else
        {
            AlertDialog.Builder alertBuilder=new      AlertDialog.Builder(UserpageActivity.this);
            alertBuilder.setTitle("Invalid Data");
            alertBuilder.setMessage("Please, Enter valid data");
            alertBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                       dialog.cancel();

                }
            });
            alertBuilder.create().show();
        }
        break;
    case R.id.breset:
        url.setText("");
        userid.setText("");
        password.setText("");
        break;
    case R.id.bcancle:
        Intent i=new Intent(getApplicationContext(),StartActivity.class);
        startActivity(i);
        finish();
        break;
    case R.id.viewsi:
        Intent ii= new   Intent(getApplicationContext(),DisplayActivity.class);
        startActivity(ii);
        finish();
        break;


    }
}

private void saveData() {
    // TODO Auto-generated method stub
    dataBase=mHelper.getWritableDatabase();
    ContentValues values=new ContentValues();

    values.put("surl",murl);
    values.put("suid", muserid);
    values.put("spassword", mpass);

    dataBase.insert(DbHelper.TABLE_NAME_SAVE, null, values);
    Toast.makeText(getApplicationContext(), "Successfully Data saved...",  Toast.LENGTH_SHORT).show();
    dataBase.close();
    Intent i=new Intent(getApplicationContext(),UserpageActivity.class);
    startActivity(i);
    //finish();
}



}

这是我的logcat .......

03-13 02:29:42.667: E/SQLiteLog(797): (1) no such table: saveinfo
03-13 02:29:42.687: E/SQLiteDatabase(797): Error inserting spassword=srikanth    suid=www.srikanth.com surl=srikanth
03-13 02:29:42.687: E/SQLiteDatabase(797): android.database.sqlite.SQLiteException: no such table: saveinfo (code 1): , while compiling: INSERT INTO saveinfo(spassword,suid,surl) VALUES (?,?,?)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at com.example.personal.UserpageActivity.saveData(UserpageActivity.java:104)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at com.example.personal.UserpageActivity.onClick(UserpageActivity.java:58)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at android.view.View.performClick(View.java:4438)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at android.view.View$PerformClick.run(View.java:18422)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at android.os.Handler.handleCallback(Handler.java:733)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at android.os.Handler.dispatchMessage(Handler.java:95)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at android.os.Looper.loop(Looper.java:136)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at android.app.ActivityThread.main(ActivityThread.java:5017)  
03-13 02:29:42.687: E/SQLiteDatabase(797):  at java.lang.reflect.Method.invokeNative(Native Method)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at java.lang.reflect.Method.invoke(Method.java:515)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-13 02:29:42.687: E/SQLiteDatabase(797):  at dalvik.system.NativeStart.main(Native Method)
03-13 02:29:42.737: I/Choreographer(797): Skipped 48 frames!  The application may be doing too much work on its main thread.
03-13 02:29:44.187: I/System.out(797): In constructor
03-13 02:29:44.597: I/Choreographer(797): Skipped 416 frames!  The application may be doing too much work on its main thread.
03-13 02:29:47.107: I/Choreographer(797): Skipped 33 frames!  The application may be doing too much work on its main thread.

当我运行UserPageActivity活动时,会显示此logcat ....并单击“保存信息”按钮...

出了什么问题?

1 个答案:

答案 0 :(得分:0)

您的CREATE TABLE存在语法问题:

public static final String SCRIPT="CREATE TABLE" + TABLE_NAME_SAVE + "(" + "_id     INTEGER PRIMARY KEY AUTOINCREMENT,"+
    "surl TEXT NOT NULL, suid TEXT NOT NULL, spassword TEXT NOT NULL);";

TABLE和名称之间需要空格,如下所示:

public static final String SCRIPT="CREATE TABLE " + TABLE_NAME_SAVE + "(" + "_id     INTEGER PRIMARY KEY AUTOINCREMENT,"+
    "surl TEXT NOT NULL, suid TEXT NOT NULL, spassword TEXT NOT NULL);";

你不应该在onCreate()中捕捉异常。当sqlite helper onCreate()成功返回时,框架认为一切正常并且表已经设置好。

如上所述修复onCreate()后,请卸载您的应用,以便再次调用onCreate()