android.database.sqlite.SQLITeException:没有这样的表:编译时管理员:INSERT INTO …等

前端之家收集整理的这篇文章主要介绍了android.database.sqlite.SQLITeException:没有这样的表:编译时管理员:INSERT INTO …等前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在我的数据库添加另一个表,第一个表已成功创建,并且数据也已成功插入.但是当我试图添加另一个表,然后插入数据…我得到了这个错误(编译INSERT时没有这样的表….等.

我在谷歌搜索,在这里也在stackoverflow,我发现一些人的代码与我的相似.有人必须添加(onUpgrade方法)到他的代码,所以它将自动增加数据库版本….但我已经写了.我感到很困惑.请帮忙.. ??

这是数据库代码

package group.com;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlException;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteOpenHelper;
import android.util.Log;

public class DBAdapter {


public static final String KEY_Admin_ROWID = "_id";
public static final String KEY_Admin_fullName = "adminFullName";
public static final String KEY_Admin_UserName = "AdminUserName";
public static final String KEY_Admin_Password = "AdminPassword";

private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "RamiTariq";

private static final String DATABASE_TABLE = "info";
private static final String DATABASE_TABLE_Admin = "admin";

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table "
    + DATABASE_TABLE + " (" + KEY_ROWID
    + " integer primary key autoincrement," 
    + KEY_NAME + " text not null," 
    + KEY_PSSWORD + " text not null," 
    + KEY_F_NAME + " text not null," 
    + KEY_DATE + " text not null," 
    + KEY_JOB + " text not null," 
    + KEY_ADDRESS + " text not null," 
    + KEY_PHONE + " text not null," 
    + KEY_NATIONAL_NUMBER + " text not null," 
    + KEY_MONEY_SHOULD_PAY + " text null," 
    + KEY_ACTUAL_MONEY + " text null);";


private static final String DATABASE_CREATE_ADMIN = "create table "
    + DATABASE_TABLE_Admin + " (" + KEY_Admin_ROWID
    + " integer primary key autoincrement," 
    + KEY_Admin_fullName + " text not null,"
    + KEY_Admin_UserName + " text not null,"
    + KEY_Admin_Password + " text not null);";


private final Context context;
private DatabaseHelper DBHelper;
private sqliteDatabase db;

public DBAdapter(Context ctx) {
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends sqliteOpenHelper {
    DatabaseHelper(Context context) {
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
    }


    public void onCreate(sqliteDatabase db) {
        try {
            db.execsql(DATABASE_CREATE);
            db.execsql(DATABASE_CREATE_ADMIN);
        } catch (sqlException e) {
            e.printStackTrace();
        }
    }

    public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
        Log.w(TAG,"Upgrading database from version " + oldVersion + " to "
                + newVersion + ",which will destroy all old        data");
        db.execsql("DROP TABLE IF EXISTS "+DATABASE_TABLE);
        db.execsql("DROP TABLE IF EXISTS "+DATABASE_TABLE_Admin);
        onCreate(db);
    }
}

// ---opens the database---
public DBAdapter open() throws sqlException {
    db = DBHelper.getWritableDatabase();
    return this;
}

// ---closes the database---
public void close() {
    DBHelper.close();
}


//------insert an admin into the database-----
public long insertAdmin(String full_name,String user_name,String password ) {
    ContentValues initialValues = new ContentValues();

    initialValues.put(KEY_Admin_fullName,full_name);
    initialValues.put(KEY_Admin_UserName,user_name);
    initialValues.put(KEY_Admin_Password,password);

    return db.insert(DATABASE_TABLE_Admin,initialValues);
}


  }

这是活动代码

package group.com;

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.Toast;


public class InsertAdmin extends Activity {

DBAdapter db = new DBAdapter(this);

EditText fullName,username,password,rePassword;
Button insertAdmin;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.insertadmin);

    fullName = (EditText) findViewById(R.id.fullName);
    username = (EditText) findViewById(R.id.username);
    password = (EditText) findViewById(R.id.password);
    rePassword = (EditText) findViewById(R.id.rePassword);

    insertAdmin = (Button) findViewById(R.id.insertAdmin);

    insertAdmin.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {

            String fName = fullName.getText().toString();
            String uName = username.getText().toString();
            String pass = password.getText().toString();
            String rePass = rePassword.getText().toString();

            if(fName.length()==0 || uName.length()==0 ||   pass.length()==0 || rePass.length()==0)
            {
                displayMessage("Please Enter Full Data");
                return;
            }

            if (uName.length() == 0)
            {
                displayMessage("Enter username");
                return;
            }
            if (pass.length() == 0)
            {
                displayMessage("Enter The password");
                return;
            }


            if(pass.equals(rePass))
            {
                db.open();
                long id = db.insertAdmin(fName,uName,pass);


                if(id>0)
                {
                    displayMessage(id + "\nSuccessfuly Inserted");

                }
                else
                {
                    displayMessage("Not Inserted");
                }

                db.close();

                startActivity(new Intent(getBaseContext(),Admin.class)) ; 

            }

            else
                displayMessage("Your Passwords doesn't match");

        }
    });

}

public void displayMessage(String msg)
{
    Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
}

  }

解决方法

卸载您的应用程序,以便删除旧版本的数据库.

您的数据库版本仍为1,因此不运行onUpgrade().数据库文件已存在,因此onCreate()不会运行.现有的数据库文件不包含您在当前版本的onCreate()中创建的所有表.

猜你在找的Android相关文章