我正在尝试在我的android应用程序中的数据库中创建两个表,创建了一个表但是没有创建第二个表。以下是代码
package com.example.visiting_card;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseHandler extends SQLiteOpenHelper {
private static String Databasename="contact.db";
private static int version=1;
private String KEY_ID="id";
private String Table_name="contact_details10";
private static String KEY_NAME="name";
private static String Email="email";
private static String Contact="contact";
private static String Company="company";
private static String address1="address1";
private static String address2="address2";
private static String State="state";
private static String Table_userpass="userpass";
private static String User="user";
private static String Pass="pass";
public DataBaseHandler(Context context)
{
super(context,Databasename, null, version);
Log.d("jhgd","sd");
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
/* String Table_create="CREATE TABLE IF NOT EXISTS "+ Table_name + "(" + KEY_ID + "
INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " text," + Email + " text,"
+ Contact +" text," + Company + " text," + address1 + " text," + address2 +
" text," + State +" text" + ")";
db.execSQL(Table_create);
Log.d("msd",Table_create);
Log.d("datbase", "datebase created");*/
}
public void table(SQLiteDatabase db){
String Table_create="CREATE TABLE IF NOT EXISTS "+ Table_name + "(" + KEY_ID + "
INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " text," + Email + " text,"
+ Contact +" text," + Company + " text," + address1 + " text," + address2 + "
text," + State +" text" + ")";
db.execSQL(Table_create);
String table_User = "CREATE TABLE " + Table_userpass + " (" + User + " TEXT PRIMARY
KEY,"+Pass+ " TEXT" + ")";
db.execSQL(table_User);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXITS " + Table_name);
Log.d("datbase", "datebase deleted");
onCreate(db);
}
public void addcontacts(Contact contact,User_Password user_pass){
SQLiteDatabase db=this.getWritableDatabase();
table(db);
ContentValues values=new ContentValues();
ContentValues values1=new ContentValues();
Log.d("name",contact.Name);
values.put(KEY_NAME, contact.Name);
values.put(Email, contact.Email);
values.put(Contact, contact.Contact);
values.put(Company,contact.Company);
values.put(address1,contact.address1);
values.put(address2, contact.addreaa2);
values.put(State, contact.State);
values1.put(User,user_pass.getuser());
values1.put(Pass, user_pass.getpassword());
db.insert(Table_name, null, values);
db.insert(Table_userpass, null, values1);
Log.d("sds", "inserting");
}
public ArrayList<Contact> getallcontact(){
ArrayList<Contact> contactlist=new ArrayList<Contact>();
SQLiteDatabase db=this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + Table_name;
Cursor cursor=db.rawQuery(selectQuery, null);
if(cursor.moveToFirst())
{
do{
Contact contact=new Contact(cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6),cursor.getString(7));
Log.d("msg",cursor.getString(0)+cursor.getString(1)+cursor.getString(2)+cursor.getString(3));
contactlist.add(contact);
}while(cursor.moveToNext());
}
return contactlist;
}
}
h在此错误中没有找到Table_useerpass的表,但是firest表是creted。 我正在使用表函数来创建表
答案 0 :(得分:0)
尝试将其声明为
旁边的变量private static final String Table_create="CREATE TABLE IF NOT EXISTS "+ Table_name + "(" + KEY_ID + "
INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " text," + Email + " text,"
+ Contact +" text," + Company + " text," + address1 + " text," + address2 + "
text," + State +" text" + ")";
private static final String table_User = "CREATE TABLE IF NOT EXISTS" + Table_userpass + " (" + User + " TEXT PRIMARY
KEY,"+ Pass + " TEXT" + ")";
并尝试在
中调用这些内容@Override
public void onCreate(SQLiteDatabase db) {
// creating required tables
db.execSQL(Table_create);
db.execSQL(table_User );
}