我有一个包含4个字段的excel文件,我想在首次使用模拟器时使用它来填充数据库。以前,我曾经将数据库文件直接存储在手机上,但如果在创建时填充这些项目会更容易。如何从excel文件或文本文件中读取并在模拟器启动时填充数据库?
答案 0 :(得分:0)
假设有一个电子表格,如: -
使用文件资源管理器将 CSV 文件 Book1.csv 保存到应用程序 < strong> asset 文件夹(如果需要,创建名为assets的文件夹),例如
即。 D:\Android_Applications\LoadDataFromExcel\app\src\main\assets
和DatabaseHelper(即SQLiteOpenHelper的子类)按照: -
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DBNAME = "mydb";
public static final int DBVERSION = 1;
public static final String MYDATA_TBL = "mydata";
public static final String MYDATA_ID_COL = "_id";
public static final String MYDATA_NAME_COL = "_name";
public static final String MYDATA_AMOUNT_COL = "_amount";
public static final String MYDATA_DATE_COL = "_date";
public static final String MYDATA_DESCRIPTION_COL = "_description";
private static final String TBLCREATESQL =
"CREATE TABLE " + MYDATA_TBL +
"(" +
MYDATA_ID_COL + " INTEGER PRIMARY KEY," +
MYDATA_NAME_COL + " TEXT," +
MYDATA_AMOUNT_COL + " INTEGER," +
MYDATA_DATE_COL + " TEXT, " +
MYDATA_DESCRIPTION_COL + " TEXT" +
")";
private SQLiteDatabase mDB;
public DatabaseHelper(Context ccontext) {
super(ccontext, DBNAME, null, DBVERSION);
mDB = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TBLCREATESQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long insertRow(String name, int amount, String date, String description) {
ContentValues cv = new ContentValues();
cv.put(MYDATA_NAME_COL,name);
cv.put(MYDATA_AMOUNT_COL,amount);
cv.put(MYDATA_DATE_COL,date);
cv.put(MYDATA_DESCRIPTION_COL,description);
return mDB.insert(MYDATA_TBL,null,cv);
}
}
然后你可以得到一些基于: -
public class MainActivity extends AppCompatActivity {
DatabaseHelper dbh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbh = new DatabaseHelper(this);
loadExternalData();
}
private void loadExternalData() {
InputStream importdata;
BufferedReader br;
String line;
String[] splitdata;
int linecount = 0;
final String tag = "loadExternalData";
try {
importdata = this.getAssets().open("Book1.csv");
} catch (IOException e) {
Log.d(tag,"Error opening asset file");
return;
}
br = new BufferedReader(new InputStreamReader(importdata));
try {
while ( (line = br.readLine()) != null) {
// Skip column headings
if (linecount++ > 0) {
splitdata = line.split(",");
dbh.insertRow(
splitdata[0],
new Integer(splitdata[1]),
splitdata[2],
splitdata[3]
);
}
}
} catch (IOException e) {
Log.d(tag,"IO Error reading data at line " + (linecount + 1));
e.printStackTrace();
}
}
}
注意!这是非常基本的,而不是原则上的证明,例如,它不会处理数据中的逗号,并且每次运行时都会创建一组新的行
1)向DatabaseHelper类添加新方法: -
public boolean ifRowExists(String name, int amount, String date, String description) {
Cursor csr = mDB.query(MYDATA_TBL,null,
MYDATA_NAME_COL + "=? AND " +
MYDATA_AMOUNT_COL + "=? AND " +
MYDATA_DATE_COL + "=? AND " +
MYDATA_DESCRIPTION_COL + "=?",
new String[]{name,Integer.toString(amount), date, description},
null,null,null
);
boolean rv = csr.getCount() > 0;
csr.close();
return rv;
}
2)修改调用类中的loadExternalData
方法以检查是否存在要插入的行: -
private void loadExternalData() {
InputStream importdata;
BufferedReader br;
String line;
String[] splitdata;
int linecount = 0;
final String tag = "loadExternalData";
try {
importdata = this.getAssets().open("Book1.csv");
} catch (IOException e) {
Log.d(tag,"Error opening asset file");
return;
}
br = new BufferedReader(new InputStreamReader(importdata));
try {
while ( (line = br.readLine()) != null) {
// Skip column headings
if (linecount++ > 0) {
splitdata = line.split(",");
if (!dbh.ifRowExists(splitdata[0], new Integer(splitdata[1]), splitdata[2], splitdata[3])) {
dbh.insertRow(
splitdata[0],
new Integer(splitdata[1]),
splitdata[2],
splitdata[3]
);
}
}
}
} catch (IOException e) {
Log.d(tag,"IO Error reading data at line " + (linecount + 1));
e.printStackTrace();
}
}