是否可以在没有分隔符的情况下将.txt文件中的数据加载到sqlite?

问题描述 投票:0回答:2

反正有没有以编程方式或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 kotlin text delimiter
2个回答
0
投票

您可以通过编程方式进行。-明智地阅读文本文件。-将其存储在sqlite DB中。


0
投票

使用Java的Regex解决您的问题。

import java.util.Arrays;

class sample{
    public static void main(String[] args) {
        String lineOfPhonesWithMultipleWhiteSpace = "0000401241   Bloody Bookmark                                       0.009780000005434     \n";
        String[] phones = lineOfPhonesWithMultipleWhiteSpace.split("\\s+");

        System.out.println("input string separated by tabs: " + lineOfPhonesWithMultipleWhiteSpace);
        System.out.println("output string: " + Arrays.toString(phones));
    }
}

输出


input string separated by unknown spaces: 0000401241   Bloody Bookmark                                       0.009780000005434     

output string: [0000401241, Bloody, Bookmark, 0.009780000005434]

© www.soinside.com 2019 - 2024. All rights reserved.