家庭记账本2
今天主要进行了连接相关数据库的操作,主要用到Android Studio中自带的SQLlite
DatabaseHelper.java
package com.example.money; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context context){ super(context,"Test.db",null,1); } //第一个参数是上下文,第二个参数是数据库名称, //第三个参数是CursorFactory对象,一般设置为null,第四个参数是数据库的版本 public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT,money VARCHAR(20),date VARCHAR(20),title VARCHAR(20),type VARCHAR(20))"); } //创建表 表名information 表结构 自增id,字符串金额、日期、详情、类别 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.d("myDeBug","数据库版本已更新"); } //数据库版本发生变化时调用 }
MoneyInfo.java
package com.example.money; public class MoneyInfo { private String money; private String date; private String title; private String type; public String getMoney() { return money; } public void setMoney(String money) { this.money = money; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getType() { return type; } public void setType(String type) { this.type = type; } @Override public String toString() { return "MoneyInfo{" + "money='" + money + '\'' + ", date='" + date + '\'' + ", title='" + title + '\''+", type='" + type + '\'' +'}'; } }