-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraft.R
67 lines (55 loc) · 1.68 KB
/
draft.R
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
library(pacman)
p_load(tidyverse,DBI,RSQLite,fs)
if(file_exists("data/TYSQL.sqlite")) {
file_delete("data/TYSQL.sqlite")
cat("EXECUTE DELETE.")
}
# 连接数据库
conn <- dbConnect(RSQLite::SQLite(), "data/TYSQL.sqlite")
# 创建数据库
read_lines("data/my_create.txt") -> sql_code
str_subset(sql_code,pattern = "^--",negate = T) %>%
map_chr(\(x) str_replace(x,"\\);$",");[FLAG]")) %>%
str_c(collapse = "\n") %>%
str_squish() %>%
str_remove("\\[FLAG\\]$") %>%
str_split("\\[FLAG\\]") %>%
unlist() -> sql_statements
# 执行 SQL 语句
for (sql in sql_statements) {
if (nchar(sql) > 0) {
tryCatch({
dbExecute(conn, sql) # 执行每条 SQL 语句
}, error = function(e) {
message("Error executing SQL: ", e$message, " in SQL: ", sql)
})
}
}
# 观察数据库中的所有表格
dbListTables(conn) -> all_tables
all_tables
lapply(all_tables, function(x) tbl(conn,x))
# 插入数据
read_lines("data/my_populate.txt") -> sql_code
str_subset(sql_code,pattern = "^--",negate = T) %>%
map_chr(\(x) str_replace(x,"\\);$",");[FLAG]")) %>%
str_c(collapse = "\n") %>%
str_squish() %>%
str_remove("\\[FLAG\\]$") %>%
str_split("\\[FLAG\\]") %>%
unlist() -> sql_statements
# 执行 SQL 语句
for (sql in sql_statements) {
if (nchar(sql) > 0) {
tryCatch({
dbExecute(conn, sql) # 执行每条 SQL 语句
}, error = function(e) {
message("Error executing SQL: ", e$message, " in SQL: ", sql)
})
}
}
# 观察数据库中的所有表格
dbListTables(conn) -> all_tables
lapply(all_tables, function(x) tbl(conn,x))
# 关闭数据库连接
dbDisconnect(conn)