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

时间:2014-01-12 00:02:51

标签: android database sqlite

我正在尝试在我的数据库中添加另一个表,第一个表已成功创建,并且数据也已成功插入。但当我试图添加另一个表,然后插入数据...我得到了这个错误(编译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, null,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();
}

  }

2 个答案:

答案 0 :(得分:14)

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

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

答案 1 :(得分:-1)

我通常使用此代码添加新表或属性

try{
        String sql = "SELECT id FROM store;";
        Cursor cursor = db.rawQuery(sql, null);
        cursor.close();
    }
    catch(SQLiteException s){  
        db.execSQL("create table store (id integer primary key, name text)"); }

首先通过选择一些数据来检查表是否存在,如果不存在则创建表