sqlite3: 用脚本批量导出sqlite数据库中的表格数据

sqlite3: 用脚本批量导出sqlite数据库中的表格数据,第1张

应用场景:kobas3.0的注释库中,有很多关系对应表。如果你单独把它提取出来,用来做自己的注释也是不错的。

假设你需要nta.db中的表格信息如何做?

第一步:查看这个文件中有哪些表格。

第二步:在txt文本中编辑好脚本信息,最后复制粘贴到shell中运行即可。

类似的内容也可以这样改。

下面的程序能将SQlite数据库信息怎么转成excel文件:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

package zhaoxing.android.tool

import java.io.File

import java.io.IOException

import jxl.Workbook

import jxl.write.Label

import jxl.write.WritableSheet

import jxl.write.WritableWorkbook

import jxl.write.WriteException

import jxl.write.biff.RowsExceededException

import android.database.Cursor

import android.database.sqlite.SQLiteDatabase

public class DatabaseDump {

private String mDestXmlFilename

private SQLiteDatabase mDb

public DatabaseDump(SQLiteDatabase db, String destXml) {

mDb = db

mDestXmlFilename = destXml

}

public void exportData() {

try {

// Log.i("mdb", mDb.getPath())

// get the tables out of the given sqlite database

String sql = "SELECT * FROM sqlite_master"

Cursor cur = mDb.rawQuery(sql, new String[0])

cur.moveToFirst()

String tableName

while (cur.getPosition() <cur.getCount()) {

tableName = cur.getString(cur.getColumnIndex("name"))

// don't process these two tables since they are used

// for metadata

if (!tableName.equals("android_metadata")

&&!tableName.equals("sqlite_sequence")) {

writeExcel(tableName)

}

cur.moveToNext()

}

} catch (Exception e) {

e.printStackTrace()

}

}

/**

* 生成一个Excel文件

*

* @param fileName

*要生成的Excel文件名

*/

public void writeExcel(String tableName) {

WritableWorkbook wwb = null

String fileName

fileName = "/sdcard/QuestionData/" + tableName + ".xls"

int r = 0

String sql = "select * from " + tableName

Cursor cur = mDb.rawQuery(sql, new String[0])

int numcols = cur.getColumnCount()

int numrows = cur.getCount()

// Log.i("row", numrows + "")

// Log.i("col", numcols + "")

String records[][] = new String[numrows + 1][numcols]// 存放答案,多一行标题行

if (cur.moveToFirst()) {

while (cur.getPosition() <cur.getCount()) {

for (int c = 0c <numcolsc++) {

if (r == 0) {

records[r][c] = cur.getColumnName(c)

records[r + 1][c] = cur.getString(c)

} else {

records[r + 1][c] = cur.getString(c)

}

// Log.i("value" + r + " " + c, records[r][c])

}

cur.moveToNext()

r++

}

cur.close()

}

try {

// 首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象

wwb = Workbook.createWorkbook(new File(fileName))

} catch (IOException e) {

e.printStackTrace()

}

if (wwb != null) {

// 创建一个可写入的工作表

// Workbook的createSheet方法有两个参数,第一个是工作表的名称,第二个是工作表在工作薄中的位置

WritableSheet ws = wwb.createSheet("sheet1", 0)

// 下面开始添加单元格

for (int i = 0i <numrows + 1i++) {

for (int j = 0j <numcolsj++) {

// 这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行

Label labelC = new Label(j, i, records[i][j])

// Log.i("Newvalue" + i + " " + j, records[i][j])

try {

// 将生成的单元格添加到工作表中

ws.addCell(labelC)

} catch (RowsExceededException e) {

e.printStackTrace()

} catch (WriteException e) {

e.printStackTrace()

}

}

}

try {

// 从内存中写入文件中

wwb.write()

// 关闭资源,释放内存

wwb.close()

} catch (IOException e) {

e.printStackTrace()

} catch (WriteException e) {

e.printStackTrace()

}

}

}

}

启动SqliteExpert后,点击左上角新增数据库,然后在d出的对话框中点击浏览2指定数据库存放的路径,并且给数据库命名,后缀名建议设置为.db3其他的保持默认设置,然后点OK4左侧显示了刚创建的数据库5点击上方菜单的新增表按钮,然后在TableName那里输入表名,然后点击下方的Add来添加表字段6在d出的对话框中设置字段名(Name),字段类型(Type),字段长度(Size),是否可空(notnull),然后点击OK依次类似添加其他字段,创建好字段后点击下方的Apply点击上方的Data,然后点击+来手动添加一行数据新增了一行双击新增的那行数据来进行编辑依次类似可以添加多条数据记录点击上方的DLL可以看到表结构的Sql语句脚本点击Design--Index--Add来添加主键索引,勾选Primary,然后选择需要作为主键的字段,点击Add,再点击OK依次类似可以添加其他的特性字段然后点击OK最后会显示设置Index的列,然后点击下方的Apply来确认设置


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6705752.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-27
下一篇 2023-03-27

发表评论

登录后才能评论

评论列表(0条)

保存