首页 > 解决方案 > 是否可以在没有分隔符的情况下将 .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(" ") 来获取分隔列,但在文本文件中,间距不同。因此,在获得行拆分器“”之后,它会将所有内容放在该空间后面并将它们放在一列中。那么我该如何解决呢?

标签: sqlitekotlintextdelimiter

解决方案


您可以以编程方式进行。- 逐行读取文本文件。- 将其存储在 sqlite DB 中。


推荐阅读