我有2个表单在单独的片段中,用户填写并将数据保存到SQLite数据库。我只用一个表,一个表单和一个片段就可以做到这一点,但现在我已经用另一个表单和片段类将第二个表(kindetails)添加到MyDBHelper类中,然后将数据保存到第二个表(kindetails)。当我填写表格并点击“addKinButtonClicked”时,我收到以下错误。
logcat的
02-23 16:40:36.891 20144-20144/com.androidbegin.absfragtabhost E/SQLiteLog﹕ (1) no such table: kindetails
02-23 16:40:36.901 20144-20144/com.androidbegin.absfragtabhost E/SQLiteDatabase﹕ Error inserting address1kin=jamaica emailkin=bobby@gmail.com address2kin=jamaica phonekin=0871234567 firstnamekin=bob surnamekin=marley youremailkin=bob@gmail.com
android.database.sqlite.SQLiteException: no such table: kindetails (code 1): , while compiling: INSERT INTO kindetails(address1kin,emailkin,address2kin,phonekin,firstnamekin,surnamekin,youremailkin) VALUES (?,?,?,?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
at com.androidbegin.absfragtabhost.MyDBHandler.addKinDetails(MyDBHandler.java:93)
at com.androidbegin.absfragtabhost.FragmentTab4.addKinButtonClicked(FragmentTab4.java:97)
at com.androidbegin.absfragtabhost.FragmentTab4$1.onClick(FragmentTab4.java:29)
at android.view.View.performClick(View.java:4456)
at android.view.View$PerformClick.run(View.java:18465)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
MyDBHelper类
package com.androidbegin.absfragtabhost;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "detailsDB.db";
public static final String TABLE_DETAILS = "details";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_FIRSTNAME = "firstname";
public static final String COLUMN_SURNAME = "surname";
public static final String COLUMN_PHONE = "phone";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_ADDRESS1 = "address1";
public static final String COLUMN_ADDRESS2 = "address2";
public static final String TABLE_KIN_DETAILS = "kindetails";
public static final String COLUMN_KIN_ID = "_id";
public static final String COLUMN_KIN_YOUREMAIL = "youremailkin";
public static final String COLUMN_KIN_FIRSTNAME = "firstnamekin";
public static final String COLUMN_KIN_SURNAME = "surnamekin";
public static final String COLUMN_KIN_PHONE = "phonekin";
public static final String COLUMN_KIN_EMAIL = "emailkin";
public static final String COLUMN_KIN_ADDRESS1 = "address1kin";
public static final String COLUMN_KIN_ADDRESS2 = "address2kin";
// Pass database information along to superclass
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = " CREATE TABLE " + TABLE_DETAILS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_FIRSTNAME + " TEXT, "
+ COLUMN_SURNAME + " TEXT, "
+ COLUMN_PHONE + " TEXT, "
+ COLUMN_EMAIL + " TEXT, "
+ COLUMN_ADDRESS1 + " TEXT, "
+ COLUMN_ADDRESS2 + " TEXT "
+ ");";
String query2 = " CREATE TABLE " + TABLE_KIN_DETAILS + "("
+ COLUMN_KIN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_KIN_YOUREMAIL + " TEXT, "
+ COLUMN_KIN_FIRSTNAME + " TEXT, "
+ COLUMN_KIN_SURNAME + " TEXT, "
+ COLUMN_KIN_PHONE + " TEXT, "
+ COLUMN_KIN_EMAIL + " TEXT, "
+ COLUMN_KIN_ADDRESS1 + " TEXT, "
+ COLUMN_KIN_ADDRESS2 + " TEXT "
+ ");";
db.execSQL(query);
db.execSQL(query2);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(" DROP TABLE IF EXISTS " + TABLE_DETAILS);
db.execSQL(" DROP TABLE IF EXISTS " + TABLE_KIN_DETAILS);
onCreate(db);
}
//Add a new row to the database
public void addDetails(Details details){
ContentValues values = new ContentValues();
values.put(COLUMN_FIRSTNAME, details.getFirstname());
values.put(COLUMN_SURNAME, details.getSurname());
values.put(COLUMN_PHONE, details.getPhone());
values.put(COLUMN_EMAIL, details.getEmail());
values.put(COLUMN_ADDRESS1, details.getAddress1());
values.put(COLUMN_ADDRESS2, details.getAddress2());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_DETAILS, null, values);
db.close();
}
public void addKinDetails(KinDetails kinDetails){
ContentValues values = new ContentValues();
values.put(COLUMN_KIN_YOUREMAIL, kinDetails.getyourEmailkin());
values.put(COLUMN_KIN_FIRSTNAME, kinDetails.getFirstnamekin());
values.put(COLUMN_KIN_SURNAME, kinDetails.getSurnamekin());
values.put(COLUMN_KIN_PHONE, kinDetails.getPhonekin());
values.put(COLUMN_KIN_EMAIL, kinDetails.getEmailkin());
values.put(COLUMN_KIN_ADDRESS1, kinDetails.getAddress1kin());
values.put(COLUMN_KIN_ADDRESS2, kinDetails.getAddress2kin());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_KIN_DETAILS, null, values);
db.close();
}
}
FragmentTab4类
package com.androidbegin.absfragtabhost;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class FragmentTab4 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmenttab4, container, false);
Button addKinButtonClicked = (Button)rootView.findViewById(R.id.addKinButtonClicked);
addKinButtonClicked.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
addKinButtonClicked(v);
}
});
//setContentView(R.layout.fragment1);
yourEmailKin = (TextView) rootView.findViewById(R.id.yourEmailKin);
editYourEmailKin = (EditText) rootView.findViewById(R.id.editYourEmailKin);
firstNameKin = (TextView) rootView.findViewById(R.id.firstNameKin);
editTextNameKin = (EditText) rootView.findViewById(R.id.editTextNameKin);
surNameKin = (TextView) rootView.findViewById(R.id.surNameKin);
editTextSurnameKin = (EditText) rootView.findViewById(R.id.editTextSurnameKin);
mobileKin = (TextView) rootView.findViewById(R.id.mobileKin);
editTextMobileKin = (EditText) rootView.findViewById(R.id.editTextMobileKin);
EmailKin = (TextView) rootView.findViewById(R.id.EmailKin);
editTextEmailKin = (EditText) rootView.findViewById(R.id.editTextEmailKin);
address1Kin = (TextView) rootView.findViewById(R.id.address1Kin);
editTextAddress1Kin = (EditText) rootView.findViewById(R.id.editTextAddress1Kin);
address2kin = (TextView) rootView.findViewById(R.id.address2Kin);
editTextAddress2Kin = (EditText) rootView.findViewById(R.id.editTextAddress2Kin);
dbHandler = new MyDBHandler(getActivity(), null, null, 1);
return rootView;
}
TextView yourEmailKin;
EditText editYourEmailKin;
TextView firstNameKin;
EditText editTextNameKin;
TextView surNameKin;
EditText editTextSurnameKin;
TextView mobileKin;
EditText editTextMobileKin;
TextView EmailKin;
EditText editTextEmailKin;
TextView address1Kin;
EditText editTextAddress1Kin;
TextView address2kin;
EditText editTextAddress2Kin;
MyDBHandler dbHandler;
//Add details to the database
public void addKinButtonClicked(View view) {
KinDetails kinDetails = new KinDetails("");
kinDetails.setYourEmailkin(editYourEmailKin.getText().toString());
kinDetails.setFirstnamekin(editTextNameKin.getText().toString());
kinDetails.setSurnamekin(editTextSurnameKin.getText().toString());
kinDetails.setPhonekin(editTextMobileKin.getText().toString());
kinDetails.setEmailkin(editTextEmailKin.getText().toString());
kinDetails.setAddress1kin(editTextAddress1Kin.getText().toString());
kinDetails.setAddress2kin(editTextAddress2Kin.getText().toString());
dbHandler.addKinDetails(kinDetails);
//printDatabase();
}
}
FragmentTab4 xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Users Email -->
<TextView
android:id="@+id/yourEmailKin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/kinyouremail"
android:layout_marginTop="35dp"/>
<EditText
android:id="@+id/editYourEmailKin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
<!-- First name -->
<TextView
android:id="@+id/firstNameKin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/kinfirstname"/>
<EditText
android:id="@+id/editTextNameKin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
<!-- Surname -->
<TextView
android:id="@+id/surNameKin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/kinsurname" />
<EditText
android:id="@+id/editTextSurnameKin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"/>
<!-- mobile -->
<TextView
android:id="@+id/mobileKin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/kinphone" />
<EditText
android:id="@+id/editTextMobileKin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"/>
<!-- Email -->
<TextView
android:id="@+id/EmailKin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/kinemail" />
<EditText
android:id="@+id/editTextEmailKin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"/>
<!-- Address1 -->
<TextView
android:id="@+id/address1Kin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/kinaddress1" />
<EditText
android:id="@+id/editTextAddress1Kin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"/>
<!-- Address2 -->
<TextView
android:id="@+id/address2Kin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/kinaddress2" />
<EditText
android:id="@+id/editTextAddress2Kin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"/>
<!-- Button -->
<Button android:id="@+id/addKinButtonClicked"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/save"
android:onClick="addKinButtonClicked"
android:layout_marginTop="40dp"/>
</LinearLayout>