问题描述
我试图添加另一个表在我的数据库,第一个成功创建,数据插入成功了。但是当我试图添加另一个表,然后插入的数据......在编译INSERT成....等我得到这个错误(没有这样的表。
我在谷歌搜索和这里太计算器,我发现了一些类似地雷的家伙用codeS。必须有人(onUpgrade法)添加到他的code所以它会自动增量数据库版本....但我已经写的。我感到很困惑。任何帮助,请??
这是数据库code:
包group.com;
进口android.content.ContentValues;
进口android.content.Context;
进口android.database.SQLException;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteOpenHelper;
进口android.util.Log;公共类DBAdapter {
公共静态最后弦乐KEY_Admin_ROWID =_id;
公共静态最后弦乐KEY_Admin_fullName =adminFullName;
公共静态最后弦乐KEY_Admin_UserName =AdminUserName;
公共静态最后弦乐KEY_Admin_Password =ADMINPASSWORD;私有静态最后弦乐TAG =DBAdapter;
私有静态最后弦乐DATABASE_NAME =RamiTariq;私有静态最后弦乐DATABASE_TABLE =信息;
私有静态最后弦乐DATABASE_TABLE_Admin =admin的;私有静态最终诠释DATABASE_VERSION = 1;
私有静态最后弦乐DATABASE_CREATE =CREATE TABLE
+ DATABASE_TABLE +(+ KEY_ROWID
+整数主键自动增量
+ KEY_NAME +文本不为空,
+ KEY_PSSWORD +文本不为空,
+ KEY_F_NAME +文本不为空,
+ KEY_DATE +文本不为空,
+ KEY_JOB +文本不为空,
+ KEY_ADDRESS +文本不为空,
+ KEY_PHONE +文本不为空,
+ KEY_NATIONAL_NUMBER +文本不为空,
+ KEY_MONEY_SHOULD_PAY +空文
+ KEY_ACTUAL_MONEY +文本NULL);
私有静态最后弦乐DATABASE_CREATE_ADMIN =CREATE TABLE
+ DATABASE_TABLE_Admin +(+ KEY_Admin_ROWID
+整数主键自动增量
+ KEY_Admin_fullName +文本不为空,
+ KEY_Admin_UserName +文本不为空,
+ KEY_Admin_Password +文字NOT NULL);;
私人最终上下文的背景下;
私人DatabaseHelper DBHelper;
私人SQLiteDatabase分贝;公共DBAdapter(上下文CTX){
this.context = CTX;
DBHelper =新DatabaseHelper(背景);
}私有静态类DatabaseHelper扩展SQLiteOpenHelper {
DatabaseHelper(上下文的背景下){
超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
}
公共无效的onCreate(SQLiteDatabase DB){
尝试{
db.execSQL(DATABASE_CREATE);
db.execSQL(DATABASE_CREATE_ADMIN);
}赶上(的SQLException E){
e.printStackTrace();
}
} 公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
Log.w(TAG,从版本升级数据库+ oldVersion +到
+ NEWVERSION +,这将摧毁所有旧数据);
db.execSQL(DROP TABLE IF EXISTS+ DATABASE_TABLE);
db.execSQL(DROP TABLE IF EXISTS+ DATABASE_TABLE_Admin);
的onCreate(DB);
}
}// ---打开数据库---
公共DBAdapter的open()抛出的SQLException {
DB = DBHelper.getWritableDatabase();
返回此;
}// ---关闭数据库---
公共无效的close(){
DBHelper.close();
}
// ------插入管理到数据库-----
众长insertAdmin(FULL_NAME字符串,字符串USER_NAME,字符串密码){
ContentValues initialValues =新ContentValues(); initialValues.put(KEY_Admin_fullName,FULL_NAME);
initialValues.put(KEY_Admin_UserName,USER_NAME);
initialValues.put(KEY_Admin_Password,密码); 返回db.insert(DATABASE_TABLE_Admin,空,initialValues);
}
}
这是活动code:
包group.com; 进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;
公共类InsertAdmin延伸活动{DBAdapter DB =新DBAdapter(本);全名的EditText,用户名,密码,确认密码;
按钮insertAdmin;公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.insertadmin); 全名=(的EditText)findViewById(R.id.fullName);
用户名=(的EditText)findViewById(R.id.username);
密码=(EditText上)findViewById(R.id.password);
确认密码=(EditText上)findViewById(R.id.rePassword); insertAdmin =(按钮)findViewById(R.id.insertAdmin); insertAdmin.setOnClickListener(新OnClickListener(){ 公共无效的onClick(查看为arg0){ 字符串FNAME = fullName.getText()的toString()。
字符串的uname = username.getText()的toString()。
。字符串传递= password.getText()的toString();
。字符串再通过= rePassword.getText()的toString(); 如果(fName.length()== 0 || uName.length()== 0 || pass.length()== 0 || rePass.length()== 0)
{
displayMessage(请输入完整的数据);
返回;
} 如果(uName.length()== 0)
{
displayMessage(输入用户名);
返回;
}
如果(pass.length()== 0)
{
displayMessage(请输入密码);
返回;
}
如果(pass.equals(REPASS))
{
db.open();
长ID = db.insertAdmin(FNAME,UNAME,通过);
如果(ID大于0)
{
displayMessage(ID +\\ nSuccessfuly插入); }
其他
{
displayMessage(不插入);
} db.close(); startActivity(新意图(getBaseContext(),Admin.class)); } 其他
displayMessage(您的密码不匹配); }
});}公共无效displayMessage(弦乐味精)
{
Toast.makeText(这一点,味精,Toast.LENGTH_SHORT).show();
} }
让老版本的数据库被删除卸载您的应用程序。
您的数据库版本仍然是 1
所以 onUpgrade()
未运行。该数据库文件已经存在,那么的onCreate()
未运行。已经存在的数据库文件不包含所有你在当前的版本创建表的onCreate()
。
i am trying to add another table in my database , the first one was created successfully, and the data was inserted successfully too. but when i tried to add another table, then inserted the data...i got this error (no such table while compiling INSERT into....etc.
i searched in google and here too in stackoverflow, i found some guys with codes similar to mine. someone had to add (onUpgrade method) to his code so it will autoincrement the database version.... but i have already wrote it. i am so confused. any help please..??
this is the database code:
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);
}
}
and this is the activity code:
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();
}
}
Uninstall your app so that the old version of the database is deleted.
Your database version is still 1
so the onUpgrade()
isn't run. The database file already exists so onCreate()
isn't run. The already existing database file doesn't contain all the tables you create in your current version of onCreate()
.
这篇关于android.database.sqlite.SQLITeException:没有这样的表:管理员在编译:INSERT INTO ...等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!