标签:ade ant 数据库表 engine href 建表 nod single integer
@
最近公司有个需求,提前让数据库表结构在excel模板中设计,然后再建对应的数据库DB。字段少好说,无脑录入即可,但是,我遇到个100多个字段的,实在忍不了,最终入门了VBA编程,自己写脚本生成了sql语句。减少了需要无用重复劳动。
首先学习下vba的基础。
alt + f11
文件 - 选项 - 自定义功能区 - 勾选开发工具
录制宏
使用相对应用录制宏
双击某个sheet页, 把鼠标放在 窗口里面,点击工具栏的插入,选择过程,随便起个过程名,比如 class就生成了代码:
Public Sub class()
End Sub
写个hello world程序:
Sub class()
Dim name
name = "Hello World"
name = "胡老师好"
MsgBox name
End Sub
在工具栏中选择 选择立即窗口,和本地窗口。
Sub class()
Dim name
name = "Hello World"
Debug.Print name
name = "胡老师好"
MsgBox name
Debug.Print name
End Sub
注释有2种形式,一种是rem。一种是 ‘ (单引号)
variant 代表任意类型
single double decimal 代表 小数、
rem 代表注释关键字
Const 常量关键字
Sub class()
Rem variant是任意类型
Dim name As Variant
name = "Hello World"
Rem debug
Debug.Print name
name = "胡老师好"
Debug.Print name
Rem 定义常量
Const num As Integer = 123
Debug.Print num
End Sub
Sub class1()
Dim x As Integer
Rem static 是静态变量
Static y As Integer
x = x + 1
y = y + 1
Debug.Print "x=" & x
Debug.Print "y=" & y
End Sub
Sub class3()
Dim number As Integer
number = 1
If number > 100 Then
Debug.Print "优秀"
ElseIf number > 95 Then
Debug.Print "良好"
Else
Debug.Print "一般"
End If
End Sub
Sub class3()
Dim number As String
number = "匹配"
If number > "匹配" Then
Debug.Print "优秀"
ElseIf number <> "匹配" Then
Debug.Print "良好"
End If
End Sub
Sub class4()
Dim number As String
number = "匹配"
Select Case number
Case "匹配"
Debug.Print "匹配"
Case "不匹配"
Debug.Print "不匹配"
End Select
End Sub
Sub class5()
Dim count As Integer
Dim if_f As Boolean
count = 11
if_f = (count = 10)
MsgBox if_f
End Sub
Sub class()
Rem 演示循环
Dim count As Integer
For count = 1 To 10
Debug.Print count
Next
Debug.Print "count 循环结束之后的值是 " & count
End Sub
循环控制单元格属性
Sub class()
Rem 演示循环
Dim count As Integer
For count = 2 To 10
If count Mod 2 = 0 Then
Rem rang函数代表选中的某列单元格
Rem Interior代表单元格内部的对象
Range("T" & count).Interior.ColorIndex = 1
Else
Range("T" & count).Interior.ColorIndex = 3
End If
Next
Debug.Print "count 循环结束之后的值是 " & count
End Sub
循环控制单元格求和
Sub class()
Rem 演示循环
Dim COUNT As Integer
Dim score As Double
For COUNT = 2 To 20
Rem cells的参数 第一个参数代表横行,第2个参数代表竖行
Cells(COUNT, 8) = "=sum(rc[-1]:rc[-6])"
Next
End Sub
Sub class()
Rem 演示循环
Dim COUNT As Integer
Dim score As Double
For COUNT = 2 To 20
Rem cells的参数 第一个参数代表横行,第2个参数代表竖行
Cells(COUNT, 8) = "=sum(rc[-1]:rc[-6])"
score = Cells(COUNT, 8)
If score > 700 Then
Cells(COUNT, 9) = "秀儿"
ElseIf score > 650 Then
Cells(COUNT, 9) = "良好"
Else
Cells(COUNT, 9) = "小垃圾"
End If
Next
End Sub
Sub class()
Rem do while 演示
Dim count As Integer
count = 20
Do While count > 10
Debug.Print count
count = count - 1
Debug.Print count
Loop
Rem do .. loop 条件 不演示了
Do
Loop While count > 10
End Sub
使用for循环的时候退出用 exit for
Sub class1()
Dim count As Integer
For count = 1 To 10
If count = 5 Then
Debug.Print "count 退出循环的值是: " & count
Exit For
End If
Debug.Print count
Next
End Sub
使用fo while循环的时候退出用 exit do
Sub class1()
Dim count As Integer
Do While True
count = count + 1
If count > 5 Then
Debug.Print "此时退出循环的值是: " & count
Exit Do
End If
Loop
End Sub
Sub class()
Dim arr(2) As Variant
Dim i As Integer
arr(0) = "小明"
arr(1) = 2
arr(2) = True
For i = 0 To 2
Debug.Print arr(i)
Next
End Sub
下标越界
Sub class()
‘ Const i As Integer = 10
‘ Dim arr(i) As Variant
Rem 可以指明数组的范围奥 起始开始限制了也是下标越界
Dim arr(2 To 5) As Variant
arr(1) = 1
Debug.Print arr(1)
End Sub
Sub class()
Dim arr(2 To 5, 3 To 6) As Variant
arr(2, 3) = 1
Debug.Print arr(2, 3)
End Sub
遍历循环
Sub class()
Dim arr(2 To 5, 3 To 6) As Variant
arr(2, 3) = 1
‘Debug.Print arr(2, 3)
For x = 2 To 5
For y = 3 To 6
Debug.Print arr(2, 3)
Next
Next
End Sub
2中方式,一种是range,一种是cells
range
cells
现在掌握了上面的基础知识,基本上可以满足我们最开始的需求
解决思路:循环方式获取单元格中的内容,拼接成 sql 的创建脚本语句即可
。
Public Sub class()
Rem 声明字段row的开始行号
Const startRow As Integer = 13
Rem 声明字段row的结束行号
Const endRow As Integer = 28
Rem 声明表名
Dim tableName As String
tableName = Range("E" & 6)
Rem 声明主键
Dim primaryKey As String
primaryKey = Range("F" & 13)
Rem 声明表名注释
Dim tableComment As String
tableComment = Range("E" & 7)
Rem 声明字段名对应列 英文序号
Dim filedMetaNo As String
filedMetaNo = "F"
Rem 声明字段注释对应对应列 英文序号
Dim commentMetaNo As String
commentMetaNo = "C"
Rem 声明字段备注 对应列的 英文序号
Dim comment2MetaNo As String
comment2MetaNo = "U"
Rem 声明类型 对应列的 英文序号
Dim typeMetaNo As String
typeMetaNo = "G"
Rem 声明字长 对应的列的英文序号
Dim lengthMetaNo As String
lengthMetaNo = "H"
Rem 最终要拼接的sql
Dim sqlStr As String
sqlStr = "CREATE TABLE " & Range("E" & 6) & Chr(13)
sqlStr = sqlStr & "(" & Chr(13)
For count = startRow To endRow
Rem 拼接 字段名
sqlStr = sqlStr & Replace(Range(filedMetaNo & count).Text, " ", "")
Rem 拼接 字段类型(字段长度)
sqlStr = sqlStr & " " & Range(typeMetaNo & count)
If IsEmpty(Range(lengthMetaNo & count)) = False Then
sqlStr = sqlStr & "(" & Range(lengthMetaNo & count) & ") "
End If
Rem 如果是主键,设置NOT NULL COMMENT
If primaryKey = Range(filedMetaNo & count) Then
sqlStr = sqlStr & " NOT NULL COMMENT "
Else
Rem 拼接 DEFAULT NULL COMMENT ‘字段名称注释(字段备注)‘
sqlStr = sqlStr & " DEFAULT NULL COMMENT "
End If
sqlStr = sqlStr & "‘" & Range(commentMetaNo & count)
If IsEmpty(Range(comment2MetaNo & count)) = False Then
sqlStr = sqlStr & "(" & Range(comment2MetaNo & count) & ")"
End If
sqlStr = sqlStr & "‘"
sqlStr = sqlStr & "," & Chr(13)
Next
sqlStr = sqlStr & "PRIMARY KEY (" & primaryKey & ")" & Chr(13)
sqlStr = sqlStr & ")ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT=‘" & tableComment & "‘" & Chr(13)
Debug.Print sqlStr
End Sub
显示结果:
在navicat中运行
显示已经把mysql数据库的表结构导入数据库了。
脚本只需要改一个地方:
对应这里的行号:
即可。
以上即是用脚本生成的4张表效果图。
个人微信公众号:
搜索: 怒放de每一天
不定时推送相关文章,期待和大家一起成长!!
完
谢谢大家支持!
使用vba程序,实现excel数据库模板生成sql数据库结构
标签:ade ant 数据库表 engine href 建表 nod single integer
原文地址:https://www.cnblogs.com/hufanglei/p/13053081.html