sqlite - 是否可以在没有分隔符的情况下将 .txt 文件中的数据加载到 sqlite?
问题描述
无论如何以编程方式或 sqlite 方式将数据从没有分隔符的文本文件加载到 sqlite 数据库?我只有没有分隔符的文本文件,如下所示 -
0000401962 0000401962
0009749467841 1000220948
0009920160010 2000021765
0009920160020 2000021786
0009920160030 2000021787
0009920160040 2000021788
0009920160042 2000024679
0009920160043 2000025073
0009920160044 2000025385
有时像这样,
0000401241 Bloody Bookmark 0.009780000005434
0000401242 ™’«®‘µ Lunch Box (§√’¡-®ÿ¥·¥ß) 139.109000021350
0000401243 ™’«®‘µ Lunch Box (‡∑“-®ÿ¥¢“«) 139.109000021351
0000401244 ‡ ◊ÈÕ¬◊¥§Õ°≈¡ ’¥” size M (¡À°√√¡ 56" 80.009000021356
0000401245 ‡ ◊ÈÕ¬◊¥§Õ°≈¡ ’¥” size L (¡À°√√¡ 56" 80.009000021357
0000401246 Àπ—ß ◊Õ·®°ø√’ ª√‘»π“§¥’ ª√“ “∑æ√–«‘À“√ 0.009000021723
0000401250 Real Parenting Box 105.009000021716
0000401251 ™ÿ¥∑’˧—Ëπ Game of Thrones 0.009000021839
0000401252 ™—Èπ«“ßÀπ—ß ◊Õ¡’≈ÈÕ ( ’‡¢’¬«) 1200.009000022269
0000401253 ™—Èπ«“ßÀπ—ß ◊Õ¡’≈ÈÕ ’¢“« 1200.009000022270
0000401254 ™—Èπ«“ßÀπ—ß ◊Õ¡’≈ÈÕ ’™¡æŸ 1200.009000022271
0000401255 ™—Èπ«“ßÀπ—ß ◊Õ¡’≈ÈÕ ( ’øÈ“) 1200.009000022272
0000401256 ™—Èπ«“ßÀπ—ß ◊Õ¡’≈ÈÕ ’‡À≈◊Õß 1200.009000022273
0000401257 Postcard °√√¡°“√¢Ë“« ÀπË«¬ : „∫ 0.009000022370
这是我尝试过的一些代码 -
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (data == null)
return
if (requestCode==requestcode) {
val filepath=data.data
val cursor=contentResolver.openInputStream(android.net.Uri.parse(filepath.toString()))
lbl.text=filepath.toString()
master_path=filepath.toString()
noti=cursor.toString()
val db=this.openOrCreateDatabase(REAL_DATABASE, Context.MODE_PRIVATE, null)
val tableName="Master"
db.execSQL("delete from $tableName")
try {
println("gg")
if (resultCode == Activity.RESULT_OK) {
try {
val file=InputStreamReader(cursor)
val buffer=BufferedReader(file)
buffer.readLine()
val contentValues=ContentValues()
db.beginTransaction()
while(true) {
val line=buffer.readLine()
if (line == null) break
val str=line.split(" ".toRegex(), 4)
.toTypedArray()
val barcode=str[0].toString()
val item_code=str[1].toString()
val onhand_qty=str[2].toString()
val description=str[3].toString()
contentValues.put("barcode", barcode)
contentValues.put("item_code", item_code)
contentValues.put("onhand_qty", onhand_qty)
contentValues.put("description", description)
db.insert(tableName, null, contentValues)
}
db.setTransactionSuccessful()
val dateF=SimpleDateFormat("dd/MM/yy", Locale.getDefault())
val date=dateF.format(Calendar.getInstance().time)
master_date=date.toString()
db.endTransaction()
summery()
} catch (e: IOException) {
if (db.inTransaction())
db.endTransaction()
val d=Dialog(this)
d.setTitle(e.message.toString() + "first")
d.show()
}
} else {
if (db.inTransaction())
db.endTransaction()
val d=Dialog(this)
d.setTitle("Only CSV files allowed")
d.show()
}
} catch (ex: Exception) {
if (db.inTransaction())
db.endTransaction()
val d=Dialog(this)
d.setTitle(ex.message.toString() + "second")
d.show()
}
}
}
如您所见,我使用 line.split(" ") 来获取分隔列,但在文本文件中,间距不同。因此,在获得行拆分器“”之后,它会将所有内容放在该空间后面并将它们放在一列中。那么我该如何解决呢?
解决方案
您可以以编程方式进行。- 逐行读取文本文件。- 将其存储在 sqlite DB 中。
推荐阅读
- sql - 在未定义外键的数据库中搜索和反向工程外键关系
- c# - 如何在表存储中播种数据?
- python - 如何将子标签添加到 lxml 的 CDATA 部分?
- javascript - 如何使脚本在应用浏览器中运行
- android - android:对 MotionLayout 子项的可见性更改
- postgresql - 将查询创建为字符串并在 PostgreSQL 10 中执行
- django - 返回渲染时的 pylint(undefined-variable) 和对象没有属性错误
- angular - 如何修复 Angular 单元测试中的“错误:没有名称的表单控件的值访问器”?
- php - Laravel,无法访问对象数据
- excel - 根据复选框和选项选择填写文本框(甚至不知道我是否以最佳方式创建它