为什么// AccountingDBHelper.java
package com.example.test_001.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class AccountingDBHelper extends SQLiteOpenHelper {
public AccountingDBHelper(@Nullable Context context) {
super(context,"tally.db" , null, 8);
}
// 表名和字段
public static final String TABLE_TRANSACTIONS = "transactions";
public static final String COLUMN_ID = "id";
public static final String COLUMN_DATE = "date";
public static final String COLUMN_TYPE = "type"; // income / expense
public static final String COLUMN_CATEGORY = "category";
public static final String COLUMN_AMOUNT = "amount";
// 创建表的 SQL 语句
private static final String CREATE_TABLE_TRANSACTIONS = "CREATE TABLE " + TABLE_TRANSACTIONS +
"(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_DATE + " TEXT, " +
COLUMN_TYPE + " TEXT, " +
COLUMN_CATEGORY + " TEXT, " +
COLUMN_AMOUNT + " REAL);";
public AccountingDBHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_TRANSACTIONS);
// 插入测试数据
db.execSQL("INSERT INTO " + TABLE_TRANSACTIONS + " (" + COLUMN_DATE + ", " + COLUMN_TYPE + ", " + COLUMN_CATEGORY + ", " + COLUMN_AMOUNT + ") VALUES ('2023-04-01', 'income', '工资', 5000.0);");
db.execSQL("INSERT INTO " + TABLE_TRANSACTIONS + " (" + COLUMN_DATE + ", " + COLUMN_TYPE + ", " + COLUMN_CATEGORY + ", " + COLUMN_AMOUNT + ") VALUES ('2023-04-02', 'expense', '餐饮', 150.0);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TRANSACTIONS);
onCreate(db);
}
/**
* 插入新的交易记录
* @param date 日期
* @param type 类型(收入/支出)
* @param category 类别
* @param amount 金额
*/
public void addTransaction(String date, String type, String category, double amount) {
SQLiteDatabase db = getWritableDatabase();
db.execSQL("INSERT INTO " + TABLE_TRANSACTIONS + " (" +
COLUMN_DATE + ", " +
COLUMN_TYPE + ", " +
COLUMN_CATEGORY + ", " +
COLUMN_AMOUNT + ") VALUES (?, ?, ?, ?)",
new String[]{date, type, category, String.valueOf(amount)});
db.close();
}
}中public AccountingDBHelper(@Nullable Context context) {会报错AccountingDBHelper(Context)' is already defined in 'com.example.test_001.db.AccountingDBHelper'而package com.hui.tally.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import com.hui.tally.R;
import java.io.File;
public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(@Nullable Context context) {
super(context,"tally.db" , null, 8);
}
/*
public static String getDatabasePath(Context context) {
File databaseFile = context.getDatabasePath("tally.db");
return databaseFile.getAbsolutePath();
}
*/
// 创建数据库的方法,只有项目第一次运行时,会被调用
@Override
public void onCreate(SQLiteDatabase db) {
// 创建表示类型的表
String sql = "create table typetb(id integer primary key autoincrement,typename varchar(10),imageId integer,sImageId integer,kind integer)";
db.execSQL(sql);
insertType(db);
//创建记账表
sql = "create table accounttb(id integer primary key autoincrement,typename varchar(10),sImageId integer,beizhu varchar(80),money float," +
"time varchar(60),year integer,month integer,day integer,kind integer)";
db.execSQL(sql);
}
private void insertType(SQLiteDatabase db) {
// 向typetb表当中插入元素
String sql = "insert into typetb (typename,imageId,sImageId,kind) values (?,?,?,?)";
db.execSQL(sql,new Object[]{"其他", R.mipmap.hs_qt_e,R.mipmap.hs_qt_f,0});
db.execSQL(sql,new Object[]{"餐饮", R.mipmap.hs_canyin_e,R.mipmap.hs_canyin_f,0});
db.execSQL(sql,new Object[]{"交通", R.mipmap.hs_jiaotong_e,R.mipmap.hs_jiaotong_f,0});
db.execSQL(sql,new Object[]{"购物", R.mipmap.hs_gouwu_e,R.mipmap.hs_gouwu_f,0});
db.execSQL(sql,new Object[]{"服饰", R.mipmap.hs_fushi_e,R.mipmap.hs_fushi_f,0});
db.execSQL(sql,new Object[]{"日用品", R.mipmap.hs_riyongpin_e,R.mipmap.hs_riyongpin_f,0});
db.execSQL(sql,new Object[]{"娱乐", R.mipmap.hs_yule_e,R.mipmap.hs_yule_f,0});
db.execSQL(sql,new Object[]{"零食", R.mipmap.hs_lingshi,R.mipmap.hs_lingshi_f,0});
db.execSQL(sql,new Object[]{"烟酒茶", R.mipmap.hs_yanjiucha_e,R.mipmap.hs_yanjiucha_f,0});
db.execSQL(sql,new Object[]{"学习", R.mipmap.hs_xuexi_e,R.mipmap.hs_xuexi_f,0});
db.execSQL(sql,new Object[]{"医疗", R.mipmap.hs_yiliao_e,R.mipmap.hs_yiliao_f,0});
db.execSQL(sql,new Object[]{"住宅", R.mipmap.hs_zhuzhai_e,R.mipmap.hs_zhuzhai_f,0});
db.execSQL(sql,new Object[]{"水电煤", R.mipmap.hs_shuidianfei_e,R.mipmap.hs_shuidianfei_f,0});
db.execSQL(sql,new Object[]{"通讯", R.mipmap.hs_tongxun_e,R.mipmap.hs_tongxun_f,0});
db.execSQL(sql,new Object[]{"人情往来", R.mipmap.hs_renqingwanglai_e,R.mipmap.hs_renqingwanglai_f,0});
db.execSQL(sql,new Object[]{"其他", R.mipmap.hs_qita_e,R.mipmap.hs_qita_f,1});
db.execSQL(sql,new Object[]{"薪资", R.mipmap.hs_xinzi_e,R.mipmap.hs_xinzi_f,1});
db.execSQL(sql,new Object[]{"奖金", R.mipmap.hs_jiangjin_e,R.mipmap.hs_jiangjin_f,1});
db.execSQL(sql,new Object[]{"借入", R.mipmap.hs_jieru_e,R.mipmap.hs_jieru_f,1});
db.execSQL(sql,new Object[]{"收债", R.mipmap.hs_shouzhai_e,R.mipmap.hs_shouzhai_f,1});
db.execSQL(sql,new Object[]{"投资回报", R.mipmap.hs_touzi_e,R.mipmap.hs_touzi_f,1});
/*
db.execSQL(sql,new Object[]{"利息收入", R.mipmap.hs_lixifuji_e,R.mipmap.hs_lixifuji_f,1});
db.execSQL(sql,new Object[]{"二手交易", R.mipmap.hs_ershoushebei_e,R.mipmap.hs_ershoushebei_f,1});
db.execSQL(sql,new Object[]{"意外所得", R.mipmap.hs_yiwai_e,R.mipmap.hs_yiwai_f,1});*/
}
// 数据库版本在更新时发生改变,会调用此方法
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < newVersion) {
// 删除旧表
db.execSQL("DROP TABLE IF EXISTS typetb");
db.execSQL("DROP TABLE IF EXISTS accounttb");
// 重新创建表并插入数据
onCreate(db);
}
}
}
中不会?
最新发布