我有两个表,类别和交易。我有单个SQLiteOpenHelper类和两个类来插入和检索类别和事务。但是我无法在事务表中插入。插入时获得负数。
DatabaseHelper Class
package com.mm.bipin.mm;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="mm";
private static final int DATABASE_VERSION=1;
public static final String CATEGORY_TABLE_NAME="category";
public static final String CATEGORY_CID="_id";
public static final String CATEGORY_TITLE= "title";
public static final String CATEGORY_DATE="date";
public static final String CATEGORY_TYPE="type";
public static final String CATEGORY_NOTE="note";
private static final String CATEGORY_CREATE_TABLE= "CREATE TABLE "+CATEGORY_TABLE_NAME+" ( "+CATEGORY_CID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
" "+CATEGORY_TITLE+" VARCHAR(255), "+CATEGORY_DATE+" VARCHAR(255),"+CATEGORY_TYPE+" INTEGER,"+CATEGORY_NOTE+" VARCHAR(255) )";
private static final String CATEGORY_DROP_TABLE=
" DROP TABLE IF EXISTS " +CATEGORY_TABLE_NAME;
public static final String TRANSACTION_TABLE_NAME="transaction";
public static final String TRANSACTION_TID="_id";
public static final String TRANSACTION_CATEGORY_ID="category_id";
public static final String TRANSACTION_TITLE="title";
public static final String TRANSACTION_AMOUNT="amount";
public static final String TRANSACTION_DATE="date";
public static final String TRANSACTION_NOTE="note";
private static final String TRANSACTION_CREATE_TABLE= "CREATE TABLE "+TRANSACTION_TABLE_NAME+" " +
" ("+TRANSACTION_TID+" INTEGER PRIMARY KEY AUTOINCREMENT, " +
" "+TRANSACTION_CATEGORY_ID+" INTEGER , " +
" "+TRANSACTION_TITLE+" VARCHAR(255) , " +
" "+TRANSACTION_AMOUNT+" VARCHAR(255) , " +
" "+TRANSACTION_DATE+" VARCHAR(255) , " +
" "+TRANSACTION_NOTE+" VARCHAR(255) ) ";
private static final String TRANSACTION_DROP_TABLE=
" DROP TABLE IF EXISTS " +TRANSACTION_TABLE_NAME;
private Context context;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME,null,DATABASE_VERSION);
this.context=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
//getWritableDatabase();
db.execSQL(CATEGORY_CREATE_TABLE);
db.execSQL(TRANSACTION_CREATE_TABLE);
Message.message(context, "DB created");
}
catch(android.database.SQLException e){
Message.message(context," "+e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try{
db.execSQL(CATEGORY_DROP_TABLE);
db.execSQL(TRANSACTION_DROP_TABLE);
Message.message(context,"DB dropped");
}
catch(android.database.SQLException e){
Message.message(context," "+e);
}
}
}
CategoryDatabaseAdapter类
package com.mm.bipin.mm.category;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
import com.mm.bipin.mm.Category;
import com.mm.bipin.mm.DatabaseHelper;
import com.mm.bipin.mm.Message;
import java.util.ArrayList;
public class CategoryDatabaseAdapter {
DatabaseHelper helper;
SQLiteDatabase db;
public static String[] columns={DatabaseHelper.CATEGORY_TITLE,DatabaseHelper.CATEGORY_DATE,DatabaseHelper.CATEGORY_NOTE};
public CategoryDatabaseAdapter(Context context){
helper=new DatabaseHelper(context);
}
Context context;
public long insertCategory(String title,int year,int month,int day,int type,String note){
db=helper.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(DatabaseHelper.CATEGORY_TITLE,title);
String date= convertDateToString(year, month, day);
contentValues.put(DatabaseHelper.CATEGORY_DATE,date);
contentValues.put(DatabaseHelper.CATEGORY_TYPE,type);
contentValues.put(DatabaseHelper.CATEGORY_NOTE,note);
long id=db.insert(DatabaseHelper.CATEGORY_TABLE_NAME,null,contentValues);
return id;
}
public Cursor getAllRows(){
db=helper.getWritableDatabase();
String[] columns={DatabaseHelper.CATEGORY_CID,DatabaseHelper.CATEGORY_TITLE,DatabaseHelper.CATEGORY_DATE,
DatabaseHelper.CATEGORY_TYPE,DatabaseHelper.CATEGORY_NOTE};
Cursor mCursor=db.query(DatabaseHelper.CATEGORY_TABLE_NAME,columns,null,null,null,null,null);
if(mCursor!=null){
mCursor.moveToNext();
}
return mCursor;
}
public String convertDateToString(int year,int month,int day){
StringBuilder str=new StringBuilder();
str.append(year);
str.append("-");
str.append(month);
str.append("-");
str.append(day);
return str.toString();
}
}
TransactionDatabaseAdapter类
package com.mm.bipin.mm.transaction;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
import com.mm.bipin.mm.DatabaseHelper;
import com.mm.bipin.mm.Message;
import com.mm.bipin.mm.category.CategoryDatabaseAdapter;
public class TransactionDatabaseAdapter {
DatabaseHelper helper;
SQLiteDatabase db;
public static String[] columns= {DatabaseHelper.TRANSACTION_TITLE,DatabaseHelper.TRANSACTION_DATE,
DatabaseHelper.TRANSACTION_NOTE};
TransactionDatabaseAdapter(Context context){
helper=new DatabaseHelper(context);
}
public long insertTransaction(String title,int category_id ,String amount,int year,int month,int day,String note){
//SQLiteDatabase db=helper.getWritableDatabase();
db=helper.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(DatabaseHelper.TRANSACTION_CATEGORY_ID,category_id);
contentValues.put(DatabaseHelper.TRANSACTION_TITLE,title);
contentValues.put(DatabaseHelper.TRANSACTION_AMOUNT,amount);
String date= convertDateToString(year, month, day);
contentValues.put(DatabaseHelper.TRANSACTION_DATE,date);
contentValues.put(DatabaseHelper.TRANSACTION_NOTE,note);
long id=db.insert(DatabaseHelper.TRANSACTION_TABLE_NAME,null,contentValues);
return id;
}
public String convertDateToString(int year,int month,int day){
StringBuilder str=new StringBuilder();
str.append(year);
str.append("-");
str.append(month);
str.append("-");
str.append(day);
return str.toString();
}
public Cursor getAllTransaction(){
db=helper.getWritableDatabase();
String[] transactionColumns={DatabaseHelper.TRANSACTION_TID,DatabaseHelper.TRANSACTION_CATEGORY_ID, DatabaseHelper.TRANSACTION_TITLE,
DatabaseHelper.TRANSACTION_AMOUNT,DatabaseHelper.TRANSACTION_DATE,DatabaseHelper.TRANSACTION_NOTE};
Cursor mCursor=db.query(DatabaseHelper.TRANSACTION_TABLE_NAME,transactionColumns,null,null,null,null,null);
if(mCursor!=null) mCursor.moveToNext();
return mCursor;
}
}
这是我的三类数据库功能。 insertTransaction()返回负值,表示未插入数据。
由于openhelper冲突可能会出现问题。但我不知道如何以及在哪里。任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
我自己找到了答案。我创建了表名' transaction'这恰好是sqlite关键字。我应该注意sql关键字。现在我将表名改为' Transactions'它现在正在工作。 三江源。