如何仅在创建时将值插入数据库?(SQLite Android Studio)
Posted
技术标签:
【中文标题】如何仅在创建时将值插入数据库?(SQLite Android Studio)【英文标题】:How to insert values into database only when created ?(SQLite Android Studio) 【发布时间】:2016-02-01 03:48:55 【问题描述】:我在我的 android studio 项目中使用 SQLite 数据库,我需要在我的数据库中的特定表的列中插入一些默认值,只有当它创建 第一次! (“第一次”-我的意思是仅在创建数据库(以及我要设置默认值的表)时!而不是在打开时(但未创建))
这是我的 dbHelper 类:
public class DBhelper extends SQLiteOpenHelper
//Table Name
public static final String TABLE_NAME = "MyShifts";
public static final String Table_NAME2="MySettings";
//Table coloumns
public static final String _ID = "_id";
public static final String MyShifts_EnteyHour = "enteryhour";
public static final String MyShifts_ExitHour = "exithour";
public static final String MyShifts_EnteryDate = "enterydate";
public static final String MyShifts_ExitDate = "exitdate";
public static final String MyShifts_Taarif="Tarrif";
public static final String MyShifts_Bonus="Bonus";
public static final String MyShifts_Comments="Comments";
public static final String MyShifts_Month="month";
public static final String MyShifts_year="year";
public static final String MyShifts_Duration="duration";
public static final String MyShifts_WeekDay="weekday";
public static final String MySettings_ID="id";
public static final String MySettings_TaarifHolDay="TaarifHol";
public static final String MySetting_TarrifFriday="TaarifFriday";
public static final String MySetting_TarrifSaturday="TaarifSatudrday";
//DataBase Information
static final String DB_NAME="Mydb.db";
// database version
static final int DB_VERSION = 1;
// Creating table query
private static final String CREATE_TABLE = "create table " + TABLE_NAME + " (" + _ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + MyShifts_EnteyHour
+ " TEXT NOT NULL, " + MyShifts_ExitHour + " TEXT NOT NULL, " + MyShifts_EnteryDate + " TEXT NOT NULL, "
+ MyShifts_ExitDate + " TEXT NOT NULL, "
+ MyShifts_Month + " INTEGER NOT NULL, " + MyShifts_year + " INTEGER NOT NULL, " + MyShifts_Taarif + " REAL NOT NULL, " + MyShifts_Bonus +
" INTEGER NOT NULL, " + MyShifts_Comments +" TEXT, " + MyShifts_Duration + " TEXT NOT NULL, " + MyShifts_WeekDay+ " TEXT NOT NULL);";
private static final String CREATE_TABLE2= "create table " + Table_NAME2 + "("+ MySettings_ID + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
MySettings_TaarifHolDay + " REAL NOT NULL, " + MySetting_TarrifFriday + " REAL NOT NULL," + MySetting_TarrifSaturday + "REAL NOT NULL);";
public DBhelper(Context context)
super(context, DB_NAME, null, DB_VERSION);
@Override
public void onCreate(SQLiteDatabase db)
db.execSQL(CREATE_TABLE);
public void OnCreate(SQLiteDatabase db)db.execSQL(CREATE_TABLE2);
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + Table_NAME2);
onCreate(db);
第一次创建“MySettings”表时(已创建但未打开),我应该添加哪些代码以及在哪里添加默认值? 谢谢!
【问题讨论】:
提示:您的onCreate()
方法实际上只创建了两个表中的一个。
第一次创建数据库的代码应该加上插入代码。
【参考方案1】:
这就是我解决这个问题的方法:
public class DatabaseHelper extends SQLiteOpenHelper
public static final String DATABASE_NAME = "Convert.db";
public static final String TABLE_DATUM="datum_table";
public static final String COL_ID="ID";
public static final String COL_NAME="NAME";
public static final String COL_A="A";
public static final String COL_B="B";
public static final String COL_SF="SCALEFACTOR";
public static final String COL_FEASTING="FEASTING";
public static final String TABLE_POINTS="point_table";
public static final String COL_PTID="PTID";
public static final String COL_PTNAME="PTNAME";
public static final String COL_NORTHING="NORTHING";
public static final String COL_EASTING="EASTING";
public static final String COL_LATITUDE="LATITUDE";
public static final String COL_LONGITUDE="LONGITUDE";
public static final String COL_CM="CM";
public static final String COL_ZONE="ZONE";
public static final String COL_DATUM="DATUMNAME";
public DatabaseHelper(Context context)
super(context, DATABASE_NAME, null, 1);
@Override
public void onCreate(SQLiteDatabase db)
db.execSQL("CREATE TABLE IF NOT EXISTS " +TABLE_DATUM +"(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT, A REAL, B REAL, SCALEFACTOR REAL, FEASTING REAL)");
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_POINTS + "(PTID INTEGER PRIMARY KEY AUTOINCREMENT,PTNAME TEXT, NORTHING REAL, EASTING REAL,LATITUDE TEXT,LONGITUDE TEXT,CM TEXT,ZONE TEXT,DATUMNAME TEXT)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('WGS 84', 6378137, 6356752.314, 0.9996, 500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('NAD 83',6378137.00,6356752.314,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('GRS 80',6378137.00,6356752.314,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('WGS 72',6378135.00,6356750.5,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('Australian 1965',6378160.00,6356774.7,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('Krasovsky 1940',6378245.00,6356863,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('North American 1927',6378206.40,6356583.8,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('International 1924',6378388.00,6356911.9,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('Hayford 1909',6378388.00,6356911.9,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('Clarke 1880',6378249.10,6356514.9,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('Clarke 1866',6378206.40,6356583.8,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('Airy 1830',6377563.40,6356256.9,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('Bessel 1841',6377397.20,6356079,0.9996,500000)");
db.execSQL("INSERT INTO " + TABLE_DATUM+ "(NAME, A, B, SCALEFACTOR, FEASTING ) VALUES ('Everest 1830',6377276.30,6356075.4,0.9996,500000)");
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DATUM);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_POINTS);
onCreate(db);
///////////////////////////////////////////////////////////////////////////////////////
public void insertDatum (String name, double a, double b, double scalefactor, double falseeasting)
SQLiteDatabase db =this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_NAME, name);
contentValues.put(COL_A, a);
contentValues.put(COL_B, b);
contentValues.put(COL_SF, scalefactor);
contentValues.put(COL_FEASTING, falseeasting);
db.insert(TABLE_DATUM, null, contentValues);
【讨论】:
【参考方案2】:我是否建议使用 db.insert() 方法与使用 execSQL 方法。查看此文档,参考这些方法及其差异:
https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insert(java.lang.String,%20java.lang.String,%20android.content.ContentValues)
首先,它不是一个 void 方法,因此您可以检查数据是否已成功插入并在必要时执行一些操作,其次它可以简化您的代码,因为您只需将 ContentValues 和 Tablename 作为 String 提供给它。看看这是否更适合你。
【讨论】:
【参考方案3】:所以我添加了这段代码,它起作用了:
@Override
public void onCreate(SQLiteDatabase db)
db.execSQL(CREATE_TABLE);
db.execSQL(CREATE_TABLE2);
ContentValues contentValues=new ContentValues();
contentValues.put(DBhelper.MySettings_TaarifHolDay,1.0);
contentValues.put(DBhelper.MySetting_TarrifFriday,1.5);
contentValues.put(DBhelper.MySetting_TarrifSaturday,1.5);
db.insert(DBhelper.Table_NAME2,null,contentValues);
【讨论】:
【参考方案4】:首先在您的代码中,不需要编写两次onCreate
方法。您可以在单个 onCreate
方法中编写两条语句,例如
@Override
public void onCreate(SQLiteDatabase db)
db.execSQL(CREATE_TABLE);
db.execSQL(CREATE_TABLE2);
// you can write your insert value code here
您可以在创建表后插入默认值。
【讨论】:
以上是关于如何仅在创建时将值插入数据库?(SQLite Android Studio)的主要内容,如果未能解决你的问题,请参考以下文章
在单击按钮时将数据插入 Android Studio 中的 SQlite 数据库
NSFetchedResultsController 仅在控制器即将插入另一个部分时将相同的单元格插入两个部分
如何通过 switch compact 将值 1 插入 sq-lite 中的列