数据库操作,为什么添加的数据后边多出很多空格!
本帖最后由 nmgwddj 于 2010-1-24 00:07 编辑我是用论坛里那个数据库操作实例修改的 这个问题具体出在哪我还真没反应过来。请高手赐教!
看上图 在网吧名称和Ip的后边 就多出... 说明是现实不完整 后边全市空格 下面是代码 帮忙看看!#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
$mdb_data_path = "sky.mdb"
$mdb_data_pwd = ""
$name = "网吧名称"
$pass = "IP"
$qq = 77777777
$e_mail = "3389"
$T = "*"
$tblname = "tywb"
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("数据库操作例子", 633, 454, 193, 115)
$ListView1 = GUICtrlCreateListView(" ID|网吧名称|IP| 密码 | 端口 ", 8, 50, 450, 350)
$Button0 = GUICtrlCreateButton("创建数据", 500, 80, 97, 25, 0)
$Button1 = GUICtrlCreateButton("写入数据", 500, 140, 97, 25, 0)
$Button2 = GUICtrlCreateButton("更新数据", 500, 200, 97, 25, 0)
$Button3 = GUICtrlCreateButton("删除数据", 500, 260, 97, 25, 0)
$Button4 = GUICtrlCreateButton("读取数据", 500, 320, 97, 25, 0)
$Button5 = GUICtrlCreateButton("添加数据", 380, 15, 80, 20, 0)
$Button6 = GUICtrlCreateButton("连接", 210, 410, 100, 30, 0)
$Button7 = GUICtrlCreateButton("取消", 330, 410, 100, 30, 0)
$mingcheng = GUICtrlCreateInput("网吧名称", 10, 15, 80, 20)
$IP = GUICtrlCreateInput("网吧IP", 100, 15, 80, 20)
$MIMA = GUICtrlCreateInput("连接密码", 190, 15, 80, 20)
$duankou = GUICtrlCreateInput("连接端口", 280, 15, 80, 20)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button0
data($mdb_data_path, $tblname)
Case $Button1;写入
sx()
du()
Case $Button2 ;更新
up()
du()
Case $Button3 ;删除
del()
du()
Case $Button4 ;读取
du()
Case $Button5 ;添加数据
tj()
Case $Button6 ;连接
mstsc()
Case $Button7 ;取消
Exit
EndSwitch
WEnd
Func du()
GUICtrlSendMsg($ListView1, $LVM_DELETEALLITEMS, 0, 0)
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path)
$RS = ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $addfld
$RS.Open("Select " & $T & " From " & $tblname)
While Not $RS.eof And Not $RS.bof
If @error = 1 Then ExitLoop
GUICtrlCreateListViewItem($RS.Fields(0).value & "|" & $RS.Fields(1).value & "|" & $RS.Fields(2).value & "|" & $RS.Fields(3).value & "|" & $RS.Fields(4).value, $ListView1)
$RS.movenext
WEnd
$RS.close
$addfld.Close
EndFunc ;==>du
Func del()
$Strn = GUICtrlRead(GUICtrlRead($ListView1))
$Strnspin = StringSplit($Strn, "|")
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path)
$sQuery = "DELETE FROM " & $tblname & " IN '" & $mdb_data_path & "' WHERE id" & " = " & $Strnspin
$addfld.execute($sQuery)
$addfld.close
MsgBox(4096, "提示:", $Strnspin & " 删除成功!!!")
EndFunc ;==>del
Func up()
$Strn = GUICtrlRead(GUICtrlRead($ListView1))
$Strnspin = StringSplit($Strn, "|")
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path)
$sQuery = "update " & $tblname & " setname='小黑Sky',pass='good',e_mail='25359272@qq.com' WHERE id" & " = " & $Strnspin
$addfld.execute($sQuery)
$addfld.close
MsgBox(4096, "提示:", $Strnspin & " 更新成功!!!")
EndFunc ;==>up
Func sx()
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & $mdb_data_path & ";Jet Oledb:Database Password=" & $mdb_data_pwd)
$x = 0
While 1
$x += 1
If $x > 10 Then ExitLoop
$addfld.Execute("insert into tywb (name,pass,qq,e_mail) values('" & $name & "','" & $pass & "','" & $qq & "','" & $e_mail & "')")
WEnd
$addfld.close
MsgBox(4096, "提示:", " 成功写入数据库!")
EndFunc ;==>sx
Func data($mdb_data_path, $tblname)
If Not FileExists($mdb_data_path) Then
$newMdb = ObjCreate("ADOX.Catalog")
$newMdb.Create("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path)
$newMdb.ActiveConnection.Close;建数据库
$addtbl = ObjCreate("ADODB.Connection")
$addtbl.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path)
$addtbl.Execute("CREATE TABLE " & $tblname)
$addtbl.Close;建表
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path)
$addfld.Execute("ALTER TABLE " & $tblname & " ADD id identity(1, 1) primary key,name char ,pass char ,qq int,e_mail char");id identity(1, 1) primary key为自动编号和主键
$addfld.Close;建列表名
MsgBox(64, "提示", "建立数据库成功", 5)
Else
MsgBox(64, "提示", "你已经建立了一个数据库了", 5)
EndIf
EndFunc ;==>data
Func tj()
$a = GUICtrlRead($mingcheng)
$b = GUICtrlRead($IP)
$c = GUICtrlRead($MIMA)
$d = GUICtrlRead($duankou)
MsgBox(64, "提示", GUICtrlRead($mingcheng), 5)
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & $mdb_data_path & ";Jet Oledb:Database Password=" & $mdb_data_pwd)
$addfld.Execute("insert into tywb (name,pass,qq,e_mail) values('" & $a & "','" & $b & "','" & $c & "','" & $d & "')")
$addfld.Close
EndFunc ;==>tj
Func mstsc()
FileDelete(@ScriptDir & "\Default.rdp")
$Strn = GUICtrlRead(GUICtrlRead($ListView1))
$Strnspin = StringSplit($Strn, "|")
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $mdb_data_path)
$rdp = _
'screen mode id:i:1' & @CRLF & _
'desktopwidth:i:1024' & @CRLF & _
'desktopheight:i:768' & @CRLF & _
'session bpp:i:15' & @CRLF & _
'winposstr:s:2,3,224,128,1024,728' & @CRLF & _
'compression:i:1' & @CRLF & _
'keyboardhook:i:2' & @CRLF & _
'displayconnectionbar:i:1' & @CRLF & _
'disable wallpaper:i:1' & @CRLF & _
'disable full window drag:i:1' & @CRLF & _
'allow desktop composition:i:0' & @CRLF & _
'allow font smoothing:i:0' & @CRLF & _
'disable menu anims:i:1' & @CRLF & _
'disable themes:i:1' & @CRLF & _
'disable cursor setting:i:0' & @CRLF & _
'bitmapcachepersistenable:i:0' & @CRLF & _
'full address:s:' & $Strnspin & ':' & $Strnspin & '' & @CRLF & _
'audiomode:i:1' & @CRLF & _
'redirectprinters:i:0' & @CRLF & _
'redirectcomports:i:0' & @CRLF & _
'redirectsmartcards:i:0' & @CRLF & _
'redirectclipboard:i:1' & @CRLF & _
'redirectposdevices:i:0' & @CRLF & _
'drivestoredirect:s:' & @CRLF & _
'autoreconnection enabled:i:1' & @CRLF & _
'authentication level:i:0' & @CRLF & _
'prompt for credentials:i:0' & @CRLF & _
'negotiate security layer:i:1' & @CRLF & _
'remoteapplicationmode:i:0' & @CRLF & _
'alternate shell:s:' & @CRLF & _
'shell working directory:s:' & @CRLF & _
'gatewayhostname:s:' & @CRLF & _
'gatewayusagemethod:i:4' & @CRLF & _
'gatewaycredentialssource:i:4' & @CRLF & _
'gatewayprofileusagemethod:i:0' & @CRLF & _
'promptcredentialonce:i:1' & @CRLF & _
'username:s:administrator' & @CRLF & _
'use multimon:i:0' & @CRLF & _
'audiocapturemode:i:0' & @CRLF & _
'videoplaybackmode:i:1' & @CRLF & _
'connection type:i:2' & @CRLF & _
'redirectdirectx:i:1' & @CRLF & _
'use redirection server name:i:0' & @CRLF & _
' '
FileWriteLine(@ScriptDir & "\Default.rdp", $rdp)
$ipdate = "Default - " & $Strnspin & ":" & $Strnspin & "- 远程桌面连接"
MsgBox(64, "提示", $ipdate, 5)
Run('c:\windows\system32\mstsc.exe /admin /edit "Default.rdp"')
WinWait("远程桌面连接")
WinActivate("远程桌面连接")
Send("!n")
WinWait($ipdate, $ipdate)
WinActivate($ipdate, $ipdate)
WinActive($ipdate, $ipdate)
WinWait($ipdate, "Input Capture Window")
WinActivate($ipdate, "Input Capture Window")
WinActive($ipdate, "Input Capture Window")
WinWait($ipdate, "Output Painter Window")
WinActivate($ipdate, "Output Painter Window")
WinActive($ipdate, "Output Painter Window")
WinWait($ipdate, "Output Painter Child Window")
WinActivate($ipdate, "Output Painter Child Window")
WinActive($ipdate, "Output Painter Child Window")
Send($Strnspin)
Send("{enter}")
EndFunc ;==>mstsc
本帖最后由 the886 于 2010-1-23 21:37 编辑
我想是在创建数据库的时候设置出问题
你可以暂时将 char(20);设置字符数上限,这样空格会少一点后面的...也会没有
还有可以改为text ;备注,没有后面的空格
还可以使用VARCHAR;也没空格
VARCHAR型和CHAR型数据的这个差别是细微的,但是非常重要。他们都是用来储存字符串长度小于255的字符。
假如你向一个长度为四十个字符的VARCHAR型字段中输入数据BIll GAtES。当你以后从这个字段中取出此数据时,你取出的数据其长度为十个字符——字符串Bill Gates的长度。 现在假如你把字符串输入一个长度为四十个字符的CHAR型字段中,那么当你取出数据时,所取出的数据长度将是四十个字符。字符串的后面会被附加多余的空格。
当你建立自己的站点时,你会发现使用VARCHAR型字段要比CHAR型字段方便的多。使用VARCHAR型字段时,你不需要为剪掉你数据中多余的空格而操心。
VARCHAR型字段的另一个突出的好处是它可以比CHAR型字段占用更少的内存和硬盘空间。当你的数据库很大时,这种内存和磁盘空间的节省会变得非常重要. 太完美了 非常非常感谢您。谢谢您 谢谢! 我想是在创建数据库的时候设置出问题
你可以暂时将 char(20);设置字符数上限,这样空格会少一点后面的.. ...
the886 发表于 2010-1-23 20:46 http://www.autoitx.com/images/common/back.gif
非常感谢。 你说的那个改类型。 是少了N多啊。
页:
[1]