gpt4 book ai didi

Android SQLite 数据库错误插入列

转载 作者:行者123 更新时间:2023-11-29 01:35:34 25 4
gpt4 key购买 nike

我创建了一个包含三列的 android 数据库,但是当我尝试保存它时出现此错误。

这是我的日志。

02-07 06:02:04.762      877-877/tubapps.datepickerdb E/SQLiteLog﹕ (1) table incomes has no column named date
02-07 06:02:04.832 877-877/tubapps.datepickerdb E/SQLiteDatabase﹕ Error inserting amount=123 date=4/2/2015 payer=McDonald's
android.database.sqlite.SQLiteException: table incomes has no column named date (code 1): , while compiling: INSERT INTO incomes(amount,date,payer) VALUES (?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
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 tubapps.datepickerdb.DBHelper.insertIncome(DBHelper.java:59)
at tubapps.datepickerdb.IncomeActivity.run(IncomeActivity.java:188)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3586)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

这是我的数据库助手

public class DBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "MyDBName.db";
public static final String INCOME_TABLE_NAME = "incomes";
public static final String INCOME_COLUMN_ID = "id";
public static final String INCOME_COLUMN_AMOUNT = "amount";
public static final String INCOME_COLUMN_PAYER = "payer";
public static final String INCOME_COLUMN_DATE = "date";


private HashMap hp;

public DBHelper(Context context)
{
super(context, DATABASE_NAME , null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table incomes " +
"(id integer primary key, amount text, payer text, date text)"
);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS incomes");
onCreate(db);
}

public boolean insertIncome (String amount, String payer, String date)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();

contentValues.put("amount", amount);
contentValues.put("payer", payer);
contentValues.put("date", date);

db.insert("incomes", null, contentValues);
return true;
}
public Cursor getData(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from incomes where id="+id+"", null );
return res;
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, INCOME_TABLE_NAME);
return numRows;
}
public boolean updateIncome (Integer id, String amount, String payer, String date)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("amount", amount);
contentValues.put("payer", payer);
contentValues.put("date", date);
db.update("incomes", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
return true;
}

public Integer deleteIncome (Integer id)
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("incomes",
"id = ? ",
new String[] { Integer.toString(id) });
}
public ArrayList getAllIncomes()
{
ArrayList array_list = new ArrayList();
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from incomes", null );
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(INCOME_COLUMN_PAYER)));
res.moveToNext();
}
return array_list;
}

}

这是我的 Activity 。

public class IncomeActivity extends ActionBarActivity {

int from_Where_I_Am_Coming = 0;
private DBHelper mydb;
TextView payer;
TextView amount;
TextView date;
int id_To_Update = 0;

private DatePicker datePicker;
private Calendar calendar;
private TextView dateView;
private int year, month, day;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_income);

payer = (TextView) findViewById(R.id.input_payer);
amount = (TextView) findViewById(R.id.input_amount);
date = (TextView) findViewById(R.id.input_date);

dateView = (TextView) findViewById(R.id.input_date);
calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
showDate(year, month+1, day);



mydb = new DBHelper(this);

Bundle extras = getIntent().getExtras();
if (extras != null) {
int Value = extras.getInt("id");
if (Value > 0) {
//means this is the view part not the add contact part.
Cursor rs = mydb.getData(Value);
id_To_Update = Value;
rs.moveToFirst();
String amo = rs.getString(rs.getColumnIndex(DBHelper.INCOME_COLUMN_AMOUNT));
String pyr = rs.getString(rs.getColumnIndex(DBHelper.INCOME_COLUMN_PAYER));
String dat = rs.getString(rs.getColumnIndex(DBHelper.INCOME_COLUMN_DATE));
if (!rs.isClosed()) {
rs.close();
}
Button save = (Button) findViewById(R.id.btn_save);
save.setVisibility(View.INVISIBLE);

Button cancel = (Button) findViewById(R.id.btn_cnc);
cancel.setVisibility(View.INVISIBLE);

amount.setText((CharSequence) amo);
amount.setFocusable(false);
amount.setClickable(false);

payer.setText((CharSequence) pyr);
payer.setFocusable(false);
payer.setClickable(false);

date.setText((CharSequence) dat);
date.setFocusable(false);
date.setClickable(false);

}
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
Bundle extras = getIntent().getExtras();
if (extras != null) {
int Value = extras.getInt("id");
if (Value > 0) {
getMenuInflater().inflate(R.menu.menu_income, menu);
} else {
getMenuInflater().inflate(R.menu.menu_main, menu);
}
}
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

switch (item.getItemId()) {
case R.id.Edit_Income:
Button save = (Button) findViewById(R.id.btn_save);
save.setVisibility(View.VISIBLE);
Button cancel = (Button) findViewById(R.id.btn_cnc);
cancel.setVisibility(View.VISIBLE);

amount.setEnabled(true);
amount.setFocusableInTouchMode(true);
amount.setClickable(true);

payer.setEnabled(true);
payer.setFocusableInTouchMode(true);
payer.setClickable(true);

date.setEnabled(true);
date.setFocusableInTouchMode(true);
date.setClickable(true);

return true;
case R.id.Delete_Income:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.deleteIncome)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mydb.deleteIncome(id_To_Update);
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), tubapps.datepickerdb.MainActivity.class);
startActivity(intent);
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog d = builder.create();
d.setTitle("Are you sure");
d.show();

return true;

default:
return super.onOptionsItemSelected(item);
}
}

public void run(View view) {
Bundle extras = getIntent().getExtras();
if (extras != null) {
int Value = extras.getInt("id");
if (Value > 0) {
if (mydb.updateIncome(id_To_Update, amount.getText().toString(), payer.getText().toString(), date.getText().toString())) {
Intent intent = new Intent(getApplicationContext(), tubapps.datepickerdb.MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "not Updated", Toast.LENGTH_SHORT).show();
}
} else {
if (mydb.insertIncome(amount.getText().toString(), payer.getText().toString(), date.getText().toString())) {
} else {
}
Intent intent = new Intent(getApplicationContext(), tubapps.datepickerdb.MainActivity.class);
startActivity(intent);
}
}
}

@SuppressWarnings("deprecation")
public void setDate(View view) {
showDialog(999);
}

@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
if (id == 999) {
return new DatePickerDialog(this, myDateListener, year, month, day);
}
return null;
}

private DatePickerDialog.OnDateSetListener myDateListener
= new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
// arg1 = year
// arg2 = month
// arg3 = day
showDate(arg1, arg2+1, arg3);
}
};

private void showDate(int year, int month, int day) {
dateView.setText(new StringBuilder().append(day).append("/")
.append(month).append("/").append(year));
}

}

有谁知道为什么我会收到这个错误,因为我不明白为什么。我已插入该列。

最佳答案

date 列可能稍后添加到 incomes 表查询中。

所以尝试传递不同的版本:

super(context, DATABASE_NAME , null, 2);

关于Android SQLite 数据库错误插入列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28378491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com