这是DBHelper类 package com.example.taxirecordapp;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context c, String dbName, int dbVer) {
super(c, dbName, null, dbVer);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String driverSQL = "CREATE TABLE DriverFile (DriverNumber TEXT PRIMARY KEY NOT NULL, DriverFName TEXT, DriverLName TEXT, DriverDateHired TEXT, DriverContactNumber TEXT, DriverAddress TEXT)";
String taxiSQL = "CREATE TABLE TaxiFile (TaxiPlateNumber TEXT PRIMARY KEY NOT NULL, TaxiDriverNumber TEXT, TaxiRentDate TEXT, TaxiDriverBalance INTEGER)";
db.execSQL(taxiSQL);
db.execSQL(driverSQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
String driverSQL = "DROP TABLE IF EXISTS DriverFile";
String taxiSQL = "DROP TABLE IF EXISTS TaxiFile";
db.execSQL(taxiSQL);
db.execSQL(driverSQL);
onCreate(db);
}
}
AddTaxi类
package com.example.taxirecordapp;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
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 AddTaxi extends Activity {
EditText etPlateNumber, etDate, etBalance, etDriverNumber;
Button btnSave, btnBack;
SQLiteDatabase dbase;
int position;
int balance = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_taxi);
DBHelper helper = new DBHelper(getApplication(), "TaxiRecordAppDB", 1);
dbase = helper.getWritableDatabase();
etPlateNumber = (EditText) findViewById(R.id.etPlateNumber);
etDate = (EditText) findViewById(R.id.etDate);
etBalance = (EditText) findViewById(R.id.etBalance);
etDriverNumber = (EditText) findViewById(R.id.etDriverNumber);
btnSave = (Button) findViewById(R.id.btnSave);
btnBack = (Button) findViewById(R.id.btnBack);
Intent i = getIntent();
position = i.getIntExtra("position", position);
Cursor rsCursor;
String[] rsFields = { "DriverNumber", "DriverFName", "DriverLName",
"DriverDateHired", "DriverContactNumber", "DriverAddress" };
rsCursor = dbase.query("DriverFile", rsFields, null, null, null, null,
null, null);
rsCursor.moveToPosition(position);
if (rsCursor.isAfterLast() == false) {
if (rsCursor.getPosition() == position) {
etDriverNumber.setText(rsCursor.getString(0).toString());
}
}
rsCursor.close();
btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (etPlateNumber.getText().toString().length() == 0) {
Toast.makeText(getApplicationContext(),
"Please input Plate Number!", Toast.LENGTH_SHORT)
.show();
} else if (etDate.getText().toString().length() == 0) {
Toast.makeText(getApplicationContext(),
"Please input Date Rented!", Toast.LENGTH_SHORT)
.show();
}
else {
if (etBalance.getText().toString().length() == 0) {
balance = 0;
} else {
balance = Integer.parseInt(etBalance.getText()
.toString());
}
AddTaxiRecord(etPlateNumber.getText().toString(),
etDriverNumber.getText().toString(), etDate
.getText().toString(), balance);
}
}
});
btnBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(AddTaxi.this, MainActivity.class);
startActivity(i);
}
});
}
public void AddTaxiRecord(String plateNumber, String driverNumber,
String date, int balance) {
try {
Cursor rsCursor;
String[] rsFields = { "TaxiPlateNumber", "TaxiDriverNumber",
"TaxiRentDate", "TaxiDriverBalance" };
rsCursor = dbase.query("TaxiFile", rsFields, "TaxiPlateNumber = "
+ plateNumber, null, null, null, null, null);
rsCursor.moveToFirst();
if (rsCursor.isAfterLast() == false) {
Toast.makeText(getApplicationContext(),
"Plate number already exist!", Toast.LENGTH_SHORT)
.show();
} else {
ContentValues rsValues = new ContentValues();
rsValues.put("TaxiPlateNumber", plateNumber);
rsValues.put("TaxiDriverNumber", driverNumber);
rsValues.put("TaxiRentDate", date);
rsValues.put("TaxiDriverBalance", balance);
dbase.insert("TaxiFile", null, rsValues);
Toast.makeText(getApplicationContext(),
"Record Successfully Saved!", Toast.LENGTH_SHORT)
.show();
}
} catch (Exception e) {
}
}
}
和正常工作的AddDriver类
package com.example.taxirecordapp;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
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 AddDriver extends Activity {
EditText etDriverNumber, etFirstName, etLastName, etDateHired,
etContactNumber, etAddress;
Button btnSave, btnBack;
SQLiteDatabase dbase;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_driver);
etDriverNumber = (EditText) findViewById(R.id.etDriverNumber);
etFirstName = (EditText) findViewById(R.id.etFirstName);
etLastName = (EditText) findViewById(R.id.etLastName);
etDateHired = (EditText) findViewById(R.id.etDateHired);
etContactNumber = (EditText) findViewById(R.id.etContactNumber);
etAddress = (EditText) findViewById(R.id.etAddress);
btnSave = (Button) findViewById(R.id.btnSave);
btnBack = (Button) findViewById(R.id.btnBack);
DBHelper helper = new DBHelper(getApplication(), "TaxiRecordAppDB", 1);
dbase = helper.getWritableDatabase();
btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (etDriverNumber.getText().toString().length() == 0) {
Toast.makeText(getApplicationContext(),
"Please input Driver Number!", Toast.LENGTH_SHORT)
.show();
} else if (etFirstName.getText().toString().length() == 0) {
Toast.makeText(getApplicationContext(),
"Please input Driver's First Name!",
Toast.LENGTH_SHORT).show();
} else if (etLastName.getText().toString().length() == 0) {
Toast.makeText(getApplicationContext(),
"Please input Driver's Last Name!",
Toast.LENGTH_SHORT).show();
} else if (etDateHired.getText().toString().length() == 0) {
Toast.makeText(getApplicationContext(),
"Please input Driver's Date Hired!",
Toast.LENGTH_SHORT).show();
} else if (etContactNumber.getText().toString().length() == 0) {
Toast.makeText(getApplicationContext(),
"Please input Driver's Contact Number!",
Toast.LENGTH_SHORT).show();
} else if (etAddress.getText().toString().length() == 0) {
Toast.makeText(getApplicationContext(),
"Please input Driver's Address!",
Toast.LENGTH_SHORT).show();
}
else {
AddDriverRecord(Integer.parseInt(etDriverNumber.getText()
.toString()), etFirstName.getText().toString(),
etLastName.getText().toString(), etDateHired
.getText().toString(), etContactNumber
.getText().toString(), etAddress.getText()
.toString());
}
}
});
btnBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(AddDriver.this,
MainActivity.class);
startActivity(i);
}
});
}
public void AddDriverRecord(int driverNumber, String firstName,
String lastName, String dateHired, String contactNumber,
String address) {
try {
Cursor rsCursor;
String[] rsFields = { "DriverNumber", "DriverFName", "DriverLName",
"DriverDateHired", "DriverContactNumber", "DriverAddress" };
rsCursor = dbase.query("DriverFile", rsFields, "DriverNumber = "
+ driverNumber, null, null, null, null, null);
rsCursor.moveToFirst();
if (rsCursor.isAfterLast() == false) {
Toast.makeText(getApplicationContext(),
"Driver number already exist!", Toast.LENGTH_SHORT)
.show();
} else {
ContentValues rsValues = new ContentValues();
rsValues.put("DriverNumber", driverNumber);
rsValues.put("DriverFName", firstName);
rsValues.put("DriverLName", lastName);
rsValues.put("DriverDateHired", dateHired);
rsValues.put("DriverContactNumber", contactNumber);
rsValues.put("DriverAddress", address);
dbase.insert("DriverFile", null, rsValues);
Toast.makeText(getApplicationContext(),
"Record Successfully Saved!", Toast.LENGTH_SHORT)
.show();
}
} catch (Exception e) {
}
}
}
答案 0 :(得分:1)
我没有看到任何问题。所以我的猜测是你添加了DriverFile表后添加了TaxiFile表。
要确保调用onUpgrade,您需要增加数据库版本,但仍然使用1:
DBHelper helper = new DBHelper(getApplication(), "TaxiRecordAppDB", 1);
这可能意味着从未创建过TaxiFile。
每次更改数据库架构都需要增加数据库版本,以确保调用onUpgrade。
编辑:
开发时,您可以执行快捷方式:保留版本1但删除设备上的应用程序或清除应用程序信息中的数据(两者都将强制全新创建数据库)。
警告:只要您发布应用程序,该快捷方式就不起作用,因为永远不要求用户这样做。在这种情况下,您确实需要自己处理架构更改和版本增量。