是否可以在没有分隔符的情况下将数据从 .txt 文件加载到 sqlite?
Is it possible to load data from .txt file to sqlite without delimiters?
有没有办法以编程方式或 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(" ") 来分隔列,但在文本文件中,间距不同。因此,在获得分线器“”之后,它会将 space 后面的所有内容放在一个列中。那我该如何解决呢?
您可以通过编程方式完成。
- 逐行读取文本文件。
- 将其存储在 sqlite 数据库中。
使用 Java 的正则表达式解决您的问题。
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]
有没有办法以编程方式或 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(" ") 来分隔列,但在文本文件中,间距不同。因此,在获得分线器“”之后,它会将 space 后面的所有内容放在一个列中。那我该如何解决呢?
您可以通过编程方式完成。 - 逐行读取文本文件。 - 将其存储在 sqlite 数据库中。
使用 Java 的正则表达式解决您的问题。
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]