我试图强制关闭插入数据库的应用程序。我认为DBAdapter类中有一些错误。在事务类中,我使用DBAdapter类的对象调用inserttransaction方法。 DBAdapter类
package com.example.kharchapaani;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBAdapter extends SQLiteOpenHelper {
private static final String DATABASE_NAME="KPdetail";
private static final int DATABASE_VERSION=1;
private static final String TABLE1_NAME="BALANCE";
private static final String TABLE2_NAME="TRANSACTION";
private static final String TABLE3_NAME="LENT";
private Context context;
private static final String KEY_NAME="NAME";
private static final String SOURCE="SOURCE";
private static final String AMOUNT="AMOUNT";
private static final String CATEGORY="CATEGORY";
private static final String DATE="DATE";
private static final String PAID="PAID";
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
String CREATE_BALANCE_TABLE="CREATE TABLE "+TABLE1_NAME +"("+SOURCE+" TEXT"+","+AMOUNT+" REAL"+")";
arg0.execSQL(CREATE_BALANCE_TABLE);
String CREATE_TRANSACTION_TABLE="CREATE TABLE "+TABLE2_NAME+"("+AMOUNT+" REAL"+","+DATE+" DATETIME"+","+CATEGORY+" TEXT"+","+SOURCE+" TEXT"+")";
arg0.execSQL(CREATE_TRANSACTION_TABLE);
String CREATE_LENT_TABLE="CREATE TABLE "+TABLE3_NAME+"("+KEY_NAME+" TEXT"+","+AMOUNT+" REAL"+","+DATE+" DATETIME"+","+SOURCE+" TEXT"+","+PAID +" INTEGER"+")";
arg0.execSQL(CREATE_LENT_TABLE);
}
public DBAdapter(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context=context;
// TODO Auto-generated constructor stub
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int oldversion, int newversion) {
// TODO Auto-generated method stub
arg0.execSQL("DROP TABLE IF EXISTS BALANCE");
arg0.execSQL("DROP TABLE IF EXISTS TRANSACTION");
arg0.execSQL("DROP TABLE IF EXISTS LENT");
onCreate(arg0);
}
void insertbalance(String source,float amount)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(SOURCE,source);
values.put(AMOUNT,amount);
db.insert(TABLE1_NAME, null, values);
db.close();
}
void inserttransaction(float amount,String date,String category,String source)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(AMOUNT,amount);
values.put(DATE,date);
values.put(CATEGORY,category);
values.put(SOURCE,source);
db.insert(TABLE2_NAME, null, values);
db.close();
}
void insertlent(String name,float amount,String date,String source,int paid)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(KEY_NAME,name);
values.put(AMOUNT,amount);
values.put(DATE,date);
values.put(SOURCE,source);
values.put(PAID,paid);
db.insert(TABLE3_NAME, null, values);
db.close();
}
public void clearData(){
context.deleteDatabase(DATABASE_NAME);
}
public Cursor getInsertedDatabalance() {
// TODO Auto-generated method stub
SQLiteDatabase db=this.getReadableDatabase();
return db.query(TABLE1_NAME,new String[] {SOURCE,AMOUNT}, null, null, null, null, null);
}
public Cursor getInsertedDatatransaction() {
// TODO Auto-generated method stub
SQLiteDatabase db=this.getReadableDatabase();
return db.query(TABLE2_NAME,new String[] {AMOUNT,DATE,CATEGORY,SOURCE}, null, null, null, null, null);
}
public Cursor getInsertedDatalentall() {
// TODO Auto-generated method stub
SQLiteDatabase db=this.getReadableDatabase();
return db.query(TABLE3_NAME,new String[] {KEY_NAME,AMOUNT,DATE,SOURCE}, null, null, null, null, null);
}
public Cursor getInsertedDatalentpaid() {
// TODO Auto-generated method stub
SQLiteDatabase db=this.getReadableDatabase();
return db.query(TABLE3_NAME,new String[] {KEY_NAME,AMOUNT,DATE,SOURCE},"PAID=1", null, null, null, null);
}
public Cursor getInsertedDatalentnotpaid() {
// TODO Auto-generated method stub
SQLiteDatabase db=this.getReadableDatabase();
return db.query(TABLE3_NAME,new String[] {KEY_NAME,AMOUNT,DATE,SOURCE},"PAID=0", null, null, null, null);
}
}
调用inserttransaction方法的类是Transaction类:
package com.example.kharchapaani;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class Transaction extends Activity {
EditText et1,et2,et3;
Button b1;
CheckBox ck1,ck2;
float amount;
String category,date,source;
Calendar cal;
DBAdapter db;
RadioGroup rg1;
RadioButton rb1;
SimpleDateFormat sdf;
ArrayList<String> list = new ArrayList<String>();
/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> adapter;
Spinner s1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transaction);
et1=(EditText)findViewById(R.id.editText1);
et2=(EditText)findViewById(R.id.editText2);
et3=(EditText)findViewById(R.id.editText3);
b1=(Button)findViewById(R.id.button1);
rg1=(RadioGroup)findViewById(R.id.radioGroup1);
db=new DBAdapter(this);
ck1=(CheckBox)findViewById(R.id.checkBox1);
ck2=(CheckBox)findViewById(R.id.checkBox2);
sdf=new SimpleDateFormat("dd/MM/yyyy");
s1=(Spinner)findViewById(R.id.spinner1);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
list.add("Laundry");
list.add("Grocery");
list.add("Entertainment");
list.add("Eating Out");
list.add("Gifts");
adapter.notifyDataSetChanged();
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{amount=Float.parseFloat(et1.getText().toString());
}
catch(Exception e)
{e.printStackTrace();}
if(ck1.isChecked()==true)
{cal = Calendar.getInstance();
date=sdf.format(cal.getTime());
}
else
{date=et2.getText().toString();}
if(ck2.isChecked()==true)
{
category=et3.getText().toString();
adapter.add(category);
adapter.notifyDataSetChanged();
}
else
{
category=String.valueOf(s1.getSelectedItem());
}
int selid=rg1.getCheckedRadioButtonId();
rb1=(RadioButton)findViewById(selid);
source=rb1.getText().toString();
db.inserttransaction(amount, date, category, source);
Toast.makeText(getBaseContext(), "Amount: "+amount+"\nDate: "+date+"\nCategory: "+category+"\nSource: "+source,Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
logcat的:
07-04 14:37:29.855: W/KeyCharacterMap(277): No keyboard for id 0
07-04 14:37:29.855: W/KeyCharacterMap(277): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
07-04 14:37:35.294: I/Database(277): sqlite returned: error code = 1, msg = near "TRANSACTION": syntax error
07-04 14:37:35.304: E/Database(277): Error inserting DATE=04/07/2015 SOURCE=Wallet CATEGORY=Laundry AMOUNT=123.0
07-04 14:37:35.304: E/Database(277): android.database.sqlite.SQLiteException: near "TRANSACTION": syntax error: , while compiling: INSERT INTO TRANSACTION(DATE, SOURCE, CATEGORY, AMOUNT) VALUES(?, ?, ?, ?);
07-04 14:37:35.304: E/Database(277): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
07-04 14:37:35.304: E/Database(277): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
07-04 14:37:35.304: E/Database(277): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
07-04 14:37:35.304: E/Database(277): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
07-04 14:37:35.304: E/Database(277): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
07-04 14:37:35.304: E/Database(277): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
07-04 14:37:35.304: E/Database(277): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1536)
07-04 14:37:35.304: E/Database(277): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
07-04 14:37:35.304: E/Database(277): at com.example.kharchapaani.DBAdapter.inserttransaction(DBAdapter.java:64)
07-04 14:37:35.304: E/Database(277): at com.example.kharchapaani.Transaction$1.onClick(Transaction.java:89)
07-04 14:37:35.304: E/Database(277): at android.view.View.performClick(View.java:2408)
07-04 14:37:35.304: E/Database(277): at android.view.View$PerformClick.run(View.java:8816)
07-04 14:37:35.304: E/Database(277): at android.os.Handler.handleCallback(Handler.java:587)
07-04 14:37:35.304: E/Database(277): at android.os.Handler.dispatchMessage(Handler.java:92)
07-04 14:37:35.304: E/Database(277): at android.os.Looper.loop(Looper.java:123)
07-04 14:37:35.304: E/Database(277): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-04 14:37:35.304: E/Database(277): at java.lang.reflect.Method.invokeNative(Native Method)
07-04 14:37:35.304: E/Database(277): at java.lang.reflect.Method.invoke(Method.java:521)
07-04 14:37:35.304: E/Database(277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-04 14:37:35.304: E/Database(277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-04 14:37:35.304: E/Database(277): at dalvik.system.NativeStart.main(Native Method)
从表和方法中删除了DATE ..仍然有一些错误..所以我猜它与DATE的数据类型没什么关系
07-04 16:20:46.815: E/Database(674): Error inserting SOURCE=Wallet AMOUNT=12.0 CATEG=Laundry
答案 0 :(得分:0)
在你的&#39;插入交易中&#39;将日期作为字符串的方法。
void inserttransaction(float amount,String date,String category,String source)
但在你的'onCreate&#39;您可以将列定义为DATETIME。
String CREATE_TRANSACTION_TABLE="CREATE TABLE "+TABLE2_NAME+"("+AMOUNT+" REAL"+","+DATE+" DATETIME"+","+CATEGORY+" TEXT"+","+SOURCE+" TEXT"+")";
DATETIME解析为键入NUMERIC,而不是TEXT 查看section 2.2 in this link
您可以执行以下操作之一:
选项A
将表定义更改为TEXT。
选项B
将日期转换为&#39; inserttransaction&#39;中的数字。方法
答案 1 :(得分:0)
您的表名与mysql的TRANSACTION表冲突。更改你的表名。
答案 2 :(得分:0)
问题是插入DATE = 04/07 / 2015
时出错在您的条目中添加双引号即“04/07/2015”,因此在插入时将您的字符串追加为“\”“+ DATE +”\“”
瞧,你的问题已经消失,你可以使用DATETIME格式