是否可以在没有分隔符的情况下将数据从.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(“”)来获得单独的列,但是在文本文件中,间距是可变的。因此,在获得行分隔符“”之后,它将所有内容都放在该空间后面,并将它们放在一列中。那我该如何解决呢?

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

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

,

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

memo

输出

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));
    }
}
本文链接:https://www.f2er.com/3068855.html

大家都在问