如何把txt文件的数据导入到sqlite里去?

tablecat123 2015-05-26 01:37:35
如何把txt文件的数据导入到sqlite里去?
...全文
1048 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
依依东望 2017-12-30
  • 打赏
  • 举报
回复
引用 1 楼 xiulongshan 的回复:
主要代码如下: try { File txt = new File(SDCARD_PATH + "/file//file.txt"); // txt文件 br = new BufferedReader(new FileReader(txt)); db = DatabaseManager.getInstance().openDatabase(); db.execSQL("create table if not exists file_table(number integer," + "name integer," + "bh char(50)," + "nl char(50)," + "jl char(50))"); Cursor cursors = db .query("file_table", null, null, null, null, null, null); if (cursors.getCount() != 0) { br.close(); cursors.close(); return; } cursors.close(); // 读取直到最后一行 String line = ""; if ((line = br.readLine()) != null) { // line = br.readLine(); } while ((line = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(line, ","); ContentValues contentValues = new ContentValues(); int i = 0; while (st.hasMoreTokens()) { // 每一行的多个字段用TAB隔开表示 String currentString = st.nextToken(); System.out.print(currentString + "\t"); if (i == 0) { contentValues.put("number", currentString); } if (i == 1) { contentValues.put("name", currentString); } if (i == 2) { contentValues.put("bh", currentString); } if (i == 3) { contentValues.put("nl", currentString); } if (i == 4) { contentValues.put("jl", currentString); } ++i; Log.v(TAG, currentString); } System.out.println(); db.insert("file_table", null, contentValues); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } }
这个导入数据的时候如果数据有重复的呢,这个可以覆盖掉吗,还有这个br是怎么定义来的?新手求指教!
qq_34586600 2016-10-12
  • 打赏
  • 举报
回复
linuxC下怎么弄啊?是不是要用fopen()函数啊,感觉还是有点麻烦啊
Leafact 2015-05-27
  • 打赏
  • 举报
回复
引用 2 楼 tablecat123 的回复:
[quote=引用 1 楼 xiulongshan 的回复:] 主要代码如下: try { File txt = new File(SDCARD_PATH + "/file//file.txt"); // txt文件 br = new BufferedReader(new FileReader(txt)); db = DatabaseManager.getInstance().openDatabase(); db.execSQL("create table if not exists file_table(number integer," + "name integer," + "bh char(50)," + "nl char(50)," + "jl char(50))"); Cursor cursors = db .query("file_table", null, null, null, null, null, null); if (cursors.getCount() != 0) { br.close(); cursors.close(); return; } cursors.close(); // 读取直到最后一行 String line = ""; if ((line = br.readLine()) != null) { // line = br.readLine(); } while ((line = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(line, ","); ContentValues contentValues = new ContentValues(); int i = 0; while (st.hasMoreTokens()) { // 每一行的多个字段用TAB隔开表示 String currentString = st.nextToken(); System.out.print(currentString + "\t"); if (i == 0) { contentValues.put("number", currentString); } if (i == 1) { contentValues.put("name", currentString); } if (i == 2) { contentValues.put("bh", currentString); } if (i == 3) { contentValues.put("nl", currentString); } if (i == 4) { contentValues.put("jl", currentString); } ++i; Log.v(TAG, currentString); } System.out.println(); db.insert("file_table", null, contentValues); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } }
如果使用空格隔开的 应该怎么改?[/quote] 读行的时候完全可以用正则判断一下spilt("一个或者多个空格")
tablecat123 2015-05-26
  • 打赏
  • 举报
回复
引用 1 楼 xiulongshan 的回复:
主要代码如下: try { File txt = new File(SDCARD_PATH + "/file//file.txt"); // txt文件 br = new BufferedReader(new FileReader(txt)); db = DatabaseManager.getInstance().openDatabase(); db.execSQL("create table if not exists file_table(number integer," + "name integer," + "bh char(50)," + "nl char(50)," + "jl char(50))"); Cursor cursors = db .query("file_table", null, null, null, null, null, null); if (cursors.getCount() != 0) { br.close(); cursors.close(); return; } cursors.close(); // 读取直到最后一行 String line = ""; if ((line = br.readLine()) != null) { // line = br.readLine(); } while ((line = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(line, ","); ContentValues contentValues = new ContentValues(); int i = 0; while (st.hasMoreTokens()) { // 每一行的多个字段用TAB隔开表示 String currentString = st.nextToken(); System.out.print(currentString + "\t"); if (i == 0) { contentValues.put("number", currentString); } if (i == 1) { contentValues.put("name", currentString); } if (i == 2) { contentValues.put("bh", currentString); } if (i == 3) { contentValues.put("nl", currentString); } if (i == 4) { contentValues.put("jl", currentString); } ++i; Log.v(TAG, currentString); } System.out.println(); db.insert("file_table", null, contentValues); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } }
如果使用空格隔开的 应该怎么改?
谷幽然 2015-05-26
  • 打赏
  • 举报
回复
主要代码如下: try { File txt = new File(SDCARD_PATH + "/file//file.txt"); // txt文件 br = new BufferedReader(new FileReader(txt)); db = DatabaseManager.getInstance().openDatabase(); db.execSQL("create table if not exists file_table(number integer," + "name integer," + "bh char(50)," + "nl char(50)," + "jl char(50))"); Cursor cursors = db .query("file_table", null, null, null, null, null, null); if (cursors.getCount() != 0) { br.close(); cursors.close(); return; } cursors.close(); // 读取直到最后一行 String line = ""; if ((line = br.readLine()) != null) { // line = br.readLine(); } while ((line = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(line, ","); ContentValues contentValues = new ContentValues(); int i = 0; while (st.hasMoreTokens()) { // 每一行的多个字段用TAB隔开表示 String currentString = st.nextToken(); System.out.print(currentString + "\t"); if (i == 0) { contentValues.put("number", currentString); } if (i == 1) { contentValues.put("name", currentString); } if (i == 2) { contentValues.put("bh", currentString); } if (i == 3) { contentValues.put("nl", currentString); } if (i == 4) { contentValues.put("jl", currentString); } ++i; Log.v(TAG, currentString); } System.out.println(); db.insert("file_table", null, contentValues); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } }

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧