'以下**用於32位系統,office 2003,環境,mysql版本5.1
'在使用前需要先安裝mysql的驅動,進行正確配置
'注意:必須給出正確的伺服器名、資料庫名、表名、資料庫連線的使用者名稱、密碼
option explicit
dim cnn as adodb.connection '定義ado連線物件
dim records as adodb.recordset '定義ado記錄集物件
'連線到資料庫
function cnnopen(byval servername as string, byval dbname as string, byval tblname as string, byval user as string, byval pwd as string) '伺服器名或ip、資料庫名、登入使用者、密碼
dim cnnstr as string '定義連線字串
set cnn = createobject("adodb.connection") '建立ado連線物件
cnn.commandtimeout = 15 '設定超時時間
cnnstr = "driver=;server=" & servername & ";database=" & dbname & ";uid=" & user & ";pwd=" & pwd & ";stmt=set names gbk" '
cnn.connectionstring = cnnstr
cnn.open
end function
'關閉連線
function cnnclose()
if cnn.state = 1 then
cnn.close
end if
end function
'取得記錄集
function getrecordset(byval sqlstr as string)
set records = createobject("adodb.recordset")
records.cursortype = adopenstatic'設定游標型別,否則無法獲得行數
records.cursorlocation = aduseclient '設定游標屬性,否則無法獲得行數
'對於connection物件的execute方法產生的記錄集物件,一般是乙個唯讀並且只向前的記錄集
'如果需要對記錄集進行操作,譬如修改和增加,則需要用乙個recordset物件
'並正確設定好cursortype和locktype為適當型別,然後呼叫open方法開啟
records.open sqlstr, cnn'使用這個語句,行數將返回-1,set records = conn.execute(sqlstr)
end function
'寫入excel表
function inputsheet(byval sheetname as string)
dim columns, rows as integer
dim i, j as integer
columns = records.fields.count
rows = records.recordcount
if records.eof = false and records.bof = false then
for i = 0 to rows - 1
for j = 0 to columns - 1
sheets(sheetname).cells(i + 2, j + 1).select
sheets(sheetname).cells(i + 2, j + 1) = records.fields.item(j).value
next
records.movenext
next
end if
sheets(sheetname).cells(1, 1).select
msgbox "output!", vbokonly, "mysql to excel"
end function
'把excel寫入mysql中的資料庫
function inserttomysql(byval sheetname as string, byval tblname as string)
dim sqlstr as string
dim i, j as integer
dim columns, rows as integer
columns = vbaproject.func_public.gettotalcolumns(sheetname)
rows = vbaproject.func_public.gettotalrows(sheetname)
set records = createobject("adodb.recordset")
'取得結果集並插入資料到資料庫
set records = createobject("adodb.recordset")
'以下語句提供了插入思路,我只是把單條記錄的插入方式改為迴圈,以把所有的記錄新增到表中
'rs.open "insert into newtable values('" & activesheet.cells(i, 1).value & "'," & "'" & activesheet.cells(i, 2).value & "')", cnn, 0
for i = 2 to rows
sqlstr = "insert into " & tblname & " values('" & sheets(sheetname).cells(i, 1).value & "'" '注意:" values('",字母「v」之前是有空格的!!!
for j = 2 to columns
sqlstr = sqlstr & ",'" & sheets(sheetname).cells(i, j).value & "'"
next
sqlstr = sqlstr & ")"
set records = cnn.execute(sqlstr) 'rs.open sqlstr, cnn, 0 不能用這條語句實現!!!
next
msgbox "insert!", vbokonly, "excel to mysql"
end function
'清除物件
function clearobj()
set cnn = nothing
set records = nothing
end function
'獲得資料表的欄位名稱
'openschema可以獲得資料庫的各種資訊
function inputcolumns(byval sheetname as string)
cnnopen "localhost", "mydb", "employees", "root", ""
set records = cnn.openschema(adschemacolumns)
dim i as integer
i = 1
while not records.eof
sheets(sheetname).cells(1, i) = records!column_name
i = i + 1
records.movenext
wend
cnnclose
clearobj
end function
編寫shell指令碼對mysql資料庫進行定時備份
在linux上安裝mysql並建立測試庫,編寫shell指令碼對此庫使用mysqldump進行定時備份和歷史備份檔案清理 保留3天 注意指令碼的通用性和擴充套件性 安裝服務端 yum install mysql server yum install mysql devel 安裝客戶端 yum ins...
總結RMySql進行R對mysql資料庫操作
總結 rmysql進行r 對mysql 資料庫操作 1 rmysql 2 連線資料庫 conn dbconnect mysql dbname mydatabase username root password 1234 host localhost port 3306 dbwritetable co...
如何用JMeter對MySQL資料庫進行壓測?
在jmeter中建立乙個jdbc request 測試計畫主要分兩步。1 設定jdbc連線池 在jdbc connection configuration 2 新增jdbc request 其他步驟就是設定引數化 斷言 監聽結果。第一步 新增 jdbc connection configuratio...