我可以改进这段代码以更快地读取和写入数据吗
我有一些我一直在玩的示例代码。基本上我只是从 csv 文件读取并写入表中。是否可以改进以下内容,使其速度更快,看起来很慢
public void impcusdata() throws SQLException, IOException {
try {
String UnitID = getUnitID();
FileReader fr = new FileReader("/mnt/sdcard/CRM.CSV");
// Put the file into the buffer
BufferedReader in = new BufferedReader(fr);
db.execSQL("DELETE FROM " + CUSTOMER_TABLE);
// Create a new string to hold the data
String data = "";
// Create the strings executing SQL commands for the data to be
// inserted
String InsertCustString = "INSERT INTO " + CUSTOMER_TABLE
+ " VALUES(";
String InsertEndString = ");";
// If there is any data in the .CSV file then read it.
while ((data = in.readLine()) != null) {
// String the results to the SQL database
db.execSQL(InsertCustString + data + InsertEndString);
}
} catch (SQLiteException ex) {
String Shaw = ex.getMessage();
}
// log entry to the LogCat file
Log.v(TAG, "Customer Table has been updated");
}
i have some sample code i have been playing around with. Basically im just reading from a csv file and writing into a table. Can the below be improved so that it goes faster seems quite slow
public void impcusdata() throws SQLException, IOException {
try {
String UnitID = getUnitID();
FileReader fr = new FileReader("/mnt/sdcard/CRM.CSV");
// Put the file into the buffer
BufferedReader in = new BufferedReader(fr);
db.execSQL("DELETE FROM " + CUSTOMER_TABLE);
// Create a new string to hold the data
String data = "";
// Create the strings executing SQL commands for the data to be
// inserted
String InsertCustString = "INSERT INTO " + CUSTOMER_TABLE
+ " VALUES(";
String InsertEndString = ");";
// If there is any data in the .CSV file then read it.
while ((data = in.readLine()) != null) {
// String the results to the SQL database
db.execSQL(InsertCustString + data + InsertEndString);
}
} catch (SQLiteException ex) {
String Shaw = ex.getMessage();
}
// log entry to the LogCat file
Log.v(TAG, "Customer Table has been updated");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,使用 SqliteDatabase.beginTransaction() 与 setTransactionSuccessful() 和 endTransaction() 您可以组合多个查询。
Yes, using SqliteDatabase.beginTransaction() with setTransactionSuccessful() and endTransaction() you can combine several queries.