我粘貼整個測試數據庫應用程序的源代碼。這應該可以解決你的問題。
我有一個活動類,將提供您的UI放置訂單和支持xml文件,和一個DbHandler類來執行數據庫操作。
這裏是活動代碼。
package com.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteConstraintException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class TestActivity extends Activity implements OnClickListener{
private EditText accountId = null;
private EditText orderNumber = null;
private Button submit = null;
private DBHandler dbHandler = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupUiElementProperties();
dbHandler = new DBHandler(this);
}
private void setupUiElementProperties() {
accountId = (EditText)findViewById(R.id.accountEditText);
orderNumber = (EditText)findViewById(R.id.orderNoEditText);
submit = (Button)findViewById(R.id.submit);
submit.setOnClickListener(this);
}
public void onClick(View v) {
try {
dbHandler.placeOrder(Integer.parseInt(accountId.getText().toString().trim()),
Integer.parseInt(orderNumber.getText().toString().trim()));
showAlert("ORDER PLACED SUCCESSFULLY");
System.out.println("DONE..");
}catch (SQLiteConstraintException ex) {
System.out.println("SQLITE EXCEPTIOn: " + ex.getMessage());
showAlert("THIS ACCOUNT DOES NOT EXISTS");
}catch (Exception e) {
System.out.println("Exceptionn: " + e.getMessage());
}
}
private void showAlert(String message) {
new AlertDialog.Builder(TestActivity.this)
.setTitle("Alert")
.setMessage(message)
.setPositiveButton("ok", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dlg, int sumthin) {}
})
.show();
}
}
這是main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/accountEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:layout_toRightOf="@+id/accountNoLabel" android:inputType="number">
<requestFocus />
</EditText>
<TextView
android:id="@+id/accountNoLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/accountEditText"
android:layout_alignBottom="@+id/accountEditText"
android:layout_alignParentLeft="true"
android:text="Account Id" android:layout_marginLeft="5dp"/>
<EditText
android:id="@+id/orderNoEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/accountEditText"
android:layout_alignRight="@+id/accountEditText"
android:layout_below="@+id/accountEditText" android:inputType="number"/>
<TextView
android:id="@+id/orderLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/orderNoEditText"
android:layout_alignBottom="@+id/orderNoEditText"
android:layout_alignLeft="@+id/accountNoLabel"
android:text="Order No" />
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Submit" />
</RelativeLayout>
最後的DbHandler類
package com.test;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHandler extends SQLiteOpenHelper {
private SQLiteDatabase sqliteDatabaseInstance_ = null;
public DBHandler(Context context){
super(context, "TESTDATABASE.db", null, 1);
sqliteDatabaseInstance_ = getWritableDatabase();
sqliteDatabaseInstance_.execSQL("PRAGMA foreign_keys = ON;");
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL("CREATE TABLE ACCOUNT (accountId INTEGER PRIMARY KEY, name TEXT)");
insertAccount(1234, "xyz", db);
db.execSQL("CREATE TABLE ORDER_DETAILS (orderNo INTEGER, accountId INTEGER," +
"FOREIGN KEY (accountId) REFERENCES ACCOUNT(accountId))");
}catch (SQLiteConstraintException sqliteConstraintException) {
System.out.println("sqliteConstraintException: " + sqliteConstraintException.getMessage());
}catch (Exception e) {
System.out.println("Exception in DBHandler.onCreate: "+e.getMessage());
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
public void placeOrder(int accountNo, int orderNo) throws SQLiteConstraintException{
ContentValues cv = new ContentValues();
cv.put("accountId", accountNo);
cv.put("orderNo", orderNo);
sqliteDatabaseInstance_.insertOrThrow("ORDER_DETAILS", null, cv);
}
private void insertAccount(int accId, String accountHolderName, SQLiteDatabase db) {
ContentValues cv = new ContentValues();
System.out.println("db: " + db);
System.out.println("accountId: " + accId) ;
System.out.println("accountHolderName: " + accountHolderName);
cv.put("accountId", accId);
cv.put("name", accountHolderName);
db.insertOrThrow("ACCOUNT", null, cv);
}
}
XML代碼實際上ü不會是人工添加的訂單號(ORDER_DETAILS表)。該字段必須自動增量並輸入數據庫。
我在DbHandler中添加了insertAccount(),因爲「Account」表沒有任何條目。使用這種方法,我添加了一個帳戶,帳戶ID爲「1234」。
這應該是烏爾輸出:
場景1 帳戶NO:1234 順序號:453412
該條目將被插入到數據庫中作爲帳戶ID 1234存在於ACCOUNT表和一個顯示成功更新警報。
方案2 賬戶2343 順序編號:14523
此項目將不會被用作外鍵約束失敗受理。所以你會得到一個警告說「這個帳戶不存在」。
我的數據庫可能與您的需求不一樣,但我希望這應該足以幫助您解決問題。
使用SQLITEBROWSER查看數據。
烏爾數據庫文件應該是下.........
轉到文件瀏覽器----->數據------>數據------> COM .test -----> TESTDATA。db
感謝的是,它解決了我問題沒問題,但我想避免在我的應用程序中有任何SQL,如果可能的話。我正在嘗試使用ORMlite解決方案來解決此問題。 – MayoMan 2012-07-25 14:57:24