Mailinglisten-Archive |
------ =_NextPart_000_01BFF87E.9D3AAFE0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit > > Ich habe mit Access97 und den MyODBC Treibern eine Access > Tabelle (kbf) auf die MySQL DB vom Server exportiert. > Exportieren und Importieren klappt insofern mit Access > (leider nur mit 97.... :-( - btw gibt es fuer 2000 eine > Export Loesung??) ganz wunderbar. >>Eine alternative Methode zum Export, der auch in Access 2000 >>hervorragend klappt: >>Unter folgender Adresse >>http://www.mysql.com/downloads/contrib.html >>gibt es ein Skript namens importsql.txt, das man ohne gro?en Aufwand >>als Modul in Access einbaut. Bei Aufruf schreibt das Modul dann alle Hi Christoph, Hier ein Modul von cynergi.net das in Access 95 - 2000 eingebunden werden kann. Rennt bei kleinen Datenbanken recht gut, wenns um grosse Datenbanken geht ;-( Gruss Wolfgang -- ** Durchgehend geoffnet: http://www.php-center.de ** Die PHP-Liste: mailto:php_(at)_php-center.de http://infosoc.uni-koeln.de/mailman/listinfo/php ------ =_NextPart_000_01BFF87E.9D3AAFE0 Content-Type: text/plain; name="export.txt" Content-Transfer-Encoding: quoted-printable Option Compare Database Option Explicit Private Const DB_ENGINE As String =3D "MY" ' USE ONLY "M1" (mSQL v1), = "M2" (mSQL v2) or "MY" (MySQL) Private Const DB_NAME As String =3D "" ' Use empty string for current. = Else use filename or DSN name of database to export Private Const DB_CONNECT As String =3D "" ' Used only if above string = is not empty Private Const MSQL_64kb_AVG As Long =3D 2048 ' ALWAYS < 65536 (to be = consistent with MS Access). Set to max expected size of Access MEMO = field (to preserve space in mSQL v1) Private Const WS_REPLACEMENT As String =3D "_" ' Use "" to simply eat = whitespaces in identifiers (table and field names) Private Const IDENT_MAX_SIZE As Integer =3D 19 ' Suggest 64. Max size = of identifiers (table and field names) Private Const PREFIX_ON_KEYWORD As String =3D "_" ' Prefix to add to = identifier, if it is a reserved word Private Const SUFFIX_ON_KEYWORD As String =3D "" ' Suffix to add to = identifier, if it is a reserved word Private Const PREFIX_ON_INDEX As String =3D "ix" ' Prefix to add to = index identifier, to make it unique (mSQL v2) Private Const SUFFIX_ON_INDEX As String =3D "" ' Suffix to add to index = identifier, to make it unique (mSQL v2) Private Const ADD_SQL_FILE As String =3D "c:\temp\esql_add.txt" ' Use = empty if open on #1. Will be overwritten if exists! Private Const DEL_SQL_FILE As String =3D "c:\temp\esql_del.txt" ' Use = empty if open on #2. Will be overwritten if exists! Private Const LINE_BREAK As String =3D "\n" ' Try "<br>". String to = replace line breaks in text fields Private Const QUERY_SEPARATOR As String =3D "\g" ' Terminator/separator = of SQL queries (to instruct some monitor program to execute them) Private Const COMMENT_PREFIX As String =3D "#" ' Use empty string for = no comments Private Const DISPLAY_WARNINGS As Boolean =3D True ' False to output = the warnings to the files, only Private Const DATE_AS_STR As Boolean =3D True ' False to use real = number data type for date, time and timestamp (in mSQL only) Private Const PARA_INSERT_AFTER As Integer =3D 3 ' Field count after = which print INSERTs different lines Private Const INDENT_SIZE As Integer =3D 5 ' Number of spaces on = indents ' Global var to store inter-funtion data Private warnings As String ' Not an option: do not set in any way ' Primary Export Function Sub exportsql() On Error GoTo exportSQL_error Dim cdb As Database Dim ctableix As Integer, ctablename As String If DB_NAME =3D "" Then Set cdb =3D CurrentDb() Else Set cdb =3D OpenDatabase(DB_NAME, False, True, DB_CONNECT) ' = Shared, read-only End If =20 If ADD_SQL_FILE <> "" Then Open ADD_SQL_FILE For Output As #1 If DEL_SQL_FILE <> "" Then Open DEL_SQL_FILE For Output As #2 DoCmd.Hourglass True If COMMENT_PREFIX <> "" Then Print #1, COMMENT_PREFIX & " Exported from MS Access to " & = IIf(Left$(DB_ENGINE, 2) =3D "MY", "MySQL", "mSQL") Print #1, COMMENT_PREFIX & " (C) 1997-98 CYNERGI - = www.cynergi.net, info_(at)_cynergi.net" Print #1, =20 Print #2, COMMENT_PREFIX & " Exported from MS Access to " & = IIf(Left$(DB_ENGINE, 2) =3D "MY", "MySQL", "mSQL") Print #2, COMMENT_PREFIX & " (C) 1997-98 CYNERGI - = www.cynergi.net, info_(at)_cynergi.net" Print #2, End If 'Go through the table definitions For ctableix =3D 0 To cdb.TableDefs.Count - 1 =20 Dim cfieldix As Integer, cfieldname As String Dim fieldlst As String, sqlcode As String Dim primary_found As Boolean Dim crs As Recordset =20 ' Let's take only the visible tables If (((cdb.TableDefs(ctableix).Attributes And DB_SYSTEMOBJECT) Or = _ (cdb.TableDefs(ctableix).Attributes And DB_HIDDENOBJECT))) =3D 0 = Then =20 ctablename =3D conv_name("" & cdb.TableDefs(ctableix).Name) =20 Print #2, Print #2, "DROP TABLE " & ctablename & QUERY_SEPARATOR =20 ' CREATE clause Print #1, Print #1, "CREATE TABLE " & ctablename Print #1, Space$(INDENT_SIZE) & "(" =20 warnings =3D "" fieldlst =3D "" primary_found =3D False =20 ' loop thorugh each field in the table For cfieldix =3D 0 To cdb.TableDefs(ctableix).Fields.Count - = 1 =20 Dim typestr As String, fieldsz As Integer, dvstr As = String Dim found_ix As Boolean, cindex As index, cfield As = Field =20 ' if this is not the first iteration, add separators If fieldlst <> "" Then fieldlst =3D fieldlst & ", " Print #1, "," End If =20 ' get field name cfieldname =3D conv_name("" & = cdb.TableDefs(ctableix).Fields(cfieldix).Name) fieldlst =3D fieldlst & cfieldname =20 ' translate types If DB_ENGINE =3D "M1" Or DB_ENGINE =3D "M2" Then Select Case = cdb.TableDefs(ctableix).Fields(cfieldix).Type Case dbChar typestr =3D "CHAR(" & = cdb.TableDefs(ctableix).Fields(cfieldix).Size & ")" Case dbText fieldsz =3D = cdb.TableDefs(ctableix).Fields(cfieldix).Size If fieldsz =3D 0 Then fieldsz =3D 255 typestr =3D "CHAR(" & fieldsz & ")" Case dbBoolean, dbByte, dbInteger, dbLong typestr =3D "INT" Case dbDouble, dbFloat, dbSingle typestr =3D "REAL" Case dbCurrency, dbDecimal, dbNumeric typestr =3D "REAL" warn "In new field '" & cfieldname & "', = currency/BCD will be converted to REAL - there may be precision loss!", = False Case dbDate typestr =3D IIf(DATE_AS_STR, "CHAR(19)", = "REAL") ' use Access internal format: IEEE 64-bit (8-byte) FP warn "In new field '" & cfieldname & "', = date/time/timestamp will be converted to " & typestr & ".", False Case dbTime typestr =3D IIf(DATE_AS_STR, "CHAR(8)", = "REAL") ' use Access internal format: IEEE 64-bit (8-byte) FP warn "In new field '" & cfieldname & "', = date/time/timestamp will be converted to " & typestr & ".", False Case dbTimeStamp typestr =3D IIf(DATE_AS_STR, "CHAR(19)", = "REAL") ' use Access internal format: IEEE 64-bit (8-byte) FP warn "In new field '" & cfieldname & "', = date/time/timestamp will be converted to " & typestr & "." & = IIf(DB_ENGINE =3D "M2", " Consider using pseudo field '_timestamp'.", = ""), False Case dbMemo If DB_ENGINE =3D "M2" Then typestr =3D "TEXT(" & MSQL_64kb_AVG & = ")" Else typestr =3D "CHAR(" & MSQL_64kb_AVG & = ")" warn "In new field '" & cfieldname & "', = dbMemo is not supported by mSQL v1 - fields larger than MSQL_64kb_AVG (" = & MSQL_64kb_AVG & ") will not be accepted!", False End If Case dbBinary, dbVarBinary typestr =3D "CHAR(255)" warn "In new field '" & cfieldname & "', = dbBinary and dbVarBinary are not supported by mSQL! - will use a text = (CHAR(255)) field.", True Case dbLongBinary typestr =3D "CHAR(" & MSQL_64kb_AVG & ")" warn "In new field '" & cfieldname & "', = dbLongBinary is not supported by mSQL! - will use a text (CHAR(" & = MSQL_64kb_AVG & ")) field.", True Case Else warn "In new field '" & cfieldname & "', = dbBigInt and dbGUID are not currently supported!", True Error 5 ' invalid Procedure Call End Select Else Select Case = cdb.TableDefs(ctableix).Fields(cfieldix).Type Case dbBinary typestr =3D "TINYBLOB" Case dbBoolean typestr =3D "TINYINT" Case dbByte typestr =3D "TINYINT UNSIGNED" Case dbChar typestr =3D "CHAR(" & = cdb.TableDefs(ctableix).Fields(cfieldix).Size & ")" Case dbCurrency typestr =3D "DECIMAL(20,4)" Case dbDate typestr =3D "DATETIME" Case dbDecimal typestr =3D "DECIMAL(20,4)" Case dbDouble typestr =3D "REAL" Case dbFloat typestr =3D "REAL" Case dbInteger typestr =3D "SMALLINT" Case dbLong typestr =3D "INT" Case dbLongBinary typestr =3D "LONGBLOB" Case dbMemo typestr =3D "LONGBLOB" ' !!!!! MySQL bug! = Replace by LONGTEXT when corrected! Case dbNumeric typestr =3D "DECIMAL(20,4)" Case dbSingle typestr =3D "FLOAT" Case dbText fieldsz =3D = cdb.TableDefs(ctableix).Fields(cfieldix).Size If fieldsz =3D 0 Then fieldsz =3D 255 typestr =3D "CHAR(" & fieldsz & ")" Case dbTime typestr =3D "TIME" Case dbTimeStamp typestr =3D "TIMESTAMP" Case dbVarBinary typestr =3D "TINYBLOB" Case dbBigInt, dbGUID warn "In new field '" & cfieldname & "', = dbBigInt and dbGUID are not currently supported!", True Error 5 ' invalid Procedure Call Case Else typestr =3D "LONGBLOB" End Select End If =20 ' check not null and auto-increment properties If ((cdb.TableDefs(ctableix).Fields(cfieldix).Attributes = And dbAutoIncrField) <> 0) Then If Left$(DB_ENGINE, 2) =3D "MY" Then typestr =3D typestr & " NOT NULL AUTO_INCREMENT" Else typestr =3D typestr & " NOT NULL" warn "In new field '" & cfieldname & "', mSQL = does not support auto-increment fields! - they will be pure INTs." & = IIf(DB_ENGINE =3D "M2", " Consider using pseudo field '_rowid' or = SEQUENCEs.", ""), False End If ElseIf cdb.TableDefs(ctableix).Fields(cfieldix).Required = =3D True Then typestr =3D typestr & " NOT NULL" End If =20 ' default value dvstr =3D = cdb.TableDefs(ctableix).Fields(cfieldix).DefaultValue If dvstr <> "" Then If Left$(DB_ENGINE, 2) <> "MY" Then warn "In new field '" & cfieldname & "', mSQL = does not support default values! - they won't be initialised.", False ElseIf = cdb.TableDefs(ctableix).Fields(cfieldix).Required =3D False Then warn "In new field '" & cfieldname & "', MySQL = needs NOT NULL to support default values! - it won't be set a default.", = False ElseIf Left$(dvstr, 1) =3D """" Then typestr =3D typestr & " DEFAULT '" & = conv_str(Mid$(dvstr, 2, Len(dvstr) - 2)) & "'" ElseIf ((LCase(dvstr) =3D "now()" Or LCase(dvstr) = =3D "date()" Or LCase(dvstr) =3D "time()") And _ (Left$(typestr, 5) =3D "DATE " Or Left$(typestr, 5) = =3D "TIME " Or Left$(typestr, 9) =3D "DATETIME ")) Then typestr =3D "TIMESTAMP " & Right$(typestr, = Len(typestr) - InStr(typestr, " ")) ElseIf LCase(dvstr) =3D "no" Then typestr =3D typestr & " DEFAULT 0" ElseIf LCase(dvstr) =3D "yes" Then typestr =3D typestr & " DEFAULT 1" Else typestr =3D typestr & " DEFAULT " & dvstr End If End If =20 ' check if primary key (for mSQL v1) If DB_ENGINE =3D "M1" Then found_ix =3D False For Each cindex In cdb.TableDefs(ctableix).Indexes If cindex.Primary Then For Each cfield In cindex.Fields If cfield.Name =3D = cdb.TableDefs(ctableix).Fields(cfieldix).Name Then found_ix =3D True Exit For End If Next cfield If found_ix Then Exit For End If Next cindex If found_ix Then If primary_found Then warn "On new table '" & ctablename & "', = mSQL v1 does not support more than one PRIMARY KEY! Only first key was = set.", False Else typestr =3D typestr & " PRIMARY KEY" primary_found =3D True End If End If End If =20 'print out field info Print #1, Space$(INDENT_SIZE) & cfieldname & = Space$(IDENT_MAX_SIZE - Len(cfieldname) + 2) & typestr; =20 Next cfieldix =20 ' terminate CREATE clause If DB_ENGINE =3D "M2" Then Print #1, Print #1, Space$(INDENT_SIZE) & ")" & QUERY_SEPARATOR End If =20 ' primary key and other index declaration If DB_ENGINE =3D "M2" Or Left$(DB_ENGINE, 2) =3D "MY" Then For Each cindex In cdb.TableDefs(ctableix).Indexes sqlcode =3D "" For Each cfield In cindex.Fields sqlcode =3D sqlcode & IIf(sqlcode =3D "", "", ", = ") & conv_name(cfield.Name) Next cfield If DB_ENGINE =3D "M2" Then Print #1, "CREATE " & IIf(cindex.unique, "UNIQUE = ", "") & "INDEX " & _ conv_name(PREFIX_ON_INDEX & cindex.Name & = SUFFIX_ON_INDEX) & " ON " & _ ctablename & " (" & sqlcode & ")" & = QUERY_SEPARATOR Else Print #1, "," Print #1, Space$(INDENT_SIZE) & = IIf(cindex.Primary, "PRIMARY ", "") & _ "KEY (" & sqlcode & ")"; End If Next cindex End If =20 ' terminate CREATE clause If DB_ENGINE <> "M2" Then Print #1, Print #1, Space$(INDENT_SIZE) & ")" & QUERY_SEPARATOR End If ' print any warnings bellow it If COMMENT_PREFIX <> "" And warnings <> "" Then If DB_ENGINE =3D "M2" Then Print #1, COMMENT_PREFIX & " = " Print #1, warnings warnings =3D "" End If =20 Print #1, =20 ' INSERT clause Set crs =3D cdb.OpenRecordset(cdb.TableDefs(ctableix).Name) If crs.RecordCount <> 0 Then =20 ' loop thorugh each record in the table crs.MoveFirst Do Until crs.EOF =20 ' start paragraphing sqlcode =3D "INSERT INTO " & ctablename If crs.Fields.Count > PARA_INSERT_AFTER Then Print #1, sqlcode If DB_ENGINE =3D "M1" Then Print #1, = Space$(INDENT_SIZE) & "(" & fieldlst & ")" Print #1, "VALUES (" sqlcode =3D Space$(INDENT_SIZE) Else If DB_ENGINE =3D "M1" Then sqlcode =3D sqlcode & = " (" & fieldlst & ")" sqlcode =3D sqlcode & " VALUES (" End If =20 ' loop through each field in each record For cfieldix =3D 0 To crs.Fields.Count - 1 =20 ' based on type, prepare the field value If IsNull(crs.Fields(cfieldix).Value) Then sqlcode =3D sqlcode & "NULL" Else Select Case crs.Fields(cfieldix).Type Case dbBoolean sqlcode =3D sqlcode & = IIf(crs.Fields(cfieldix).Value =3D True, "1", "0") Case dbChar, dbText, dbMemo sqlcode =3D sqlcode & "'" & = conv_str(crs.Fields(cfieldix).Value) & "'" Case dbDate, dbTimeStamp If Left$(DB_ENGINE, 2) =3D "MY" Or = DATE_AS_STR Then sqlcode =3D sqlcode & "'" & = Format(crs.Fields(cfieldix).Value, "YYYY-MM-DD HH:MM:SS") & "'" Else 'print in Access internal = format: IEEE 64-bit (8-byte) FP sqlcode =3D sqlcode & "'" & = Format(crs.Fields(cfieldix).Value, "#.#########") & "'" End If Case dbTime If Left$(DB_ENGINE, 2) =3D "MY" Or = DATE_AS_STR Then sqlcode =3D sqlcode & "'" & = Format(crs.Fields(cfieldix).Value, "HH:MM:SS") & "'" Else 'print in Access internal = format: IEEE 64-bit (8-byte) FP sqlcode =3D sqlcode & "'" & = Format(crs.Fields(cfieldix).Value, "#.#########") & "'" End If Case dbBinary, dbLongBinary, dbVarBinary sqlcode =3D sqlcode & "'" & = conv_bin(crs.Fields(cfieldix).Value) & "'" Case Else sqlcode =3D sqlcode & = conv_str(crs.Fields(cfieldix).Value) End Select End If =20 ' paragraph separators If cfieldix < crs.Fields.Count - 1 Then sqlcode =3D sqlcode & ", " If crs.Fields.Count > PARA_INSERT_AFTER Then Print #1, sqlcode sqlcode =3D Space$(INDENT_SIZE) End If End If =20 Next cfieldix =20 ' print out result and any warnings sqlcode =3D sqlcode & IIf(crs.Fields.Count > = PARA_INSERT_AFTER, " )", ")") & QUERY_SEPARATOR Print #1, sqlcode If COMMENT_PREFIX <> "" And warnings <> "" Then Print #1, warnings warnings =3D "" End If If crs.Fields.Count > PARA_INSERT_AFTER Then Print = #1, =20 crs.MoveNext Loop =20 Else =20 ' if there is no data on the table If COMMENT_PREFIX <> "" Then Print #1, COMMENT_PREFIX & = " This table has no data" =20 End If =20 crs.close Set crs =3D Nothing =20 End If 'print only unhidden tables =20 Next ctableix =20 exportSQL_exit: Close #2 Close #1 =20 cdb.close Set cdb =3D Nothing DoCmd.Hourglass False Exit Sub exportSQL_error: MsgBox Err.Description Resume exportSQL_exit End Sub Private Function conv_name(strname As String) As String Dim I As Integer, str As String ' replace inner spaces with WS_REPLACEMENT str =3D strname I =3D 1 While I <=3D Len(str) Select Case Mid$(str, I, 1) Case " ", Chr$(9), Chr$(10), Chr$(13) ' space, tab, = newline, carriage return str =3D Left$(str, I - 1) & WS_REPLACEMENT & Right$(str, = Len(str) - I) I =3D I + Len(WS_REPLACEMENT) Case Else I =3D I + 1 End Select Wend ' restrict tablename to IDENT_MAX_SIZE chars, *after* eating spaces str =3D Left$(str, IDENT_MAX_SIZE) ' check for reserved words conv_name =3D str If Left$(DB_ENGINE, 2) =3D "MY" Then Select Case LCase$(str) Case "add", "all", "alter", "and", "as", "asc", = "auto_increment", "between", _ "bigint", "binary", "blob", "both", "by", "cascade", "char", = "character", _ "change", "check", "column", "columns", "create", "data", = "datetime", "dec", _ "decimal", "default", "delete", "desc", "describe", = "distinct", "double", _ "drop", "escaped", "enclosed", "explain", "fields", "float", = "float4", _ "float8", "foreign", "from", "for", "full", "grant", = "group", "having", _ "ignore", "in", "index", "infile", "insert", "int", = "integer", "interval", _ "int1", "int2", "int3", "int4", "int8", "into", "is", "key", = "keys", _ "leading", "like", "lines", "limit", "lock", "load", "long", = "longblob", _ "longtext", "match", "mediumblob", "mediumtext", = "mediumint", "middleint", _ "numeric", "not", "null", "on", "option", "optionally", = "or", "order", _ "outfile", "partial", "precision", "primary", "procedure", = "privileges", _ "read", "real", "references", "regexp", "repeat", "replace", = "restrict", _ "rlike", "select", "set", "show", "smallint", = "sql_big_tables", _ "sql_big_selects", "sql_select_limit", "straight_join", = "table", "tables", _ "terminated", "tinyblob", "tinytext", "tinyint", "trailing", = "to", "unique", _ "unlock", "unsigned", "update", "usage", "values", = "varchar", "varying", _ "with", "write", "where", "zerofill" conv_name =3D Left$(PREFIX_ON_KEYWORD & str & = SUFFIX_ON_KEYWORD, IDENT_MAX_SIZE) If (str =3D conv_name) Then warn "In identifier '" & strname & "', the new form = '" & strname & _ "' is a reserved word, and PREFIX_ON_KEYWORD ('" & _ PREFIX_ON_KEYWORD & "') and SUFFIX_ON_KEYWORD ('" & = SUFFIX_ON_KEYWORD & _ "') make it larger than IDENT_MAX_SIZE, and after = cut it is the same as the original! " & _ "This is usually caused by a void or empty = PREFIX_ON_KEYWORD.", True Error 5 ' invalid Procedure Call End If End Select End If End Function Private Function conv_str(str As String) As String Dim I As Integer, nlstr As String, rstr As Variant =20 nlstr =3D "" rstr =3D Null I =3D 1 While I <=3D Len(str) Select Case Mid$(str, I, 1) Case Chr$(0) ' ASCII NUL nlstr =3D "" rstr =3D "\0" Case Chr$(8) ' backspace nlstr =3D "" rstr =3D "\b" Case Chr$(9) ' tab nlstr =3D "" rstr =3D "\t" Case "'" nlstr =3D "" rstr =3D "\'" Case """" nlstr =3D "" rstr =3D "\""" Case "\" nlstr =3D "" rstr =3D "\\" Case Chr$(10), Chr$(13) ' line feed and carriage return If nlstr <> "" And nlstr <> Mid$(str, I, 1) Then ' there was a previous newline and this is its pair: = eat it rstr =3D "" nlstr =3D "" Else ' this is a fresh newline rstr =3D LINE_BREAK nlstr =3D Mid$(str, I, 1) End If Case Else nlstr =3D "" End Select If Not IsNull(rstr) Then str =3D Left$(str, I - 1) & rstr & Right$(str, Len(str) - I) I =3D I + Len(rstr) rstr =3D Null Else I =3D I + 1 End If Wend conv_str =3D str End Function Private Function conv_bin(str As String) As String Dim I As Integer, rstr As String =20 rstr =3D "" I =3D 1 While I <=3D Len(str) Select Case Mid$(str, I, 1) Case Chr$(0) ' ASCII NUL rstr =3D "\0" Case Chr$(8) ' backspace rstr =3D "\b" Case Chr$(9) ' tab rstr =3D "\t" Case "'" rstr =3D "\'" Case """" rstr =3D "\""" Case "\" rstr =3D "\\" Case Chr$(10) ' line feed rstr =3D "\n" Case Chr$(13) ' carriage return rstr =3D "\r" End Select If rstr <> "" Then str =3D Left$(str, I - 1) & rstr & Right$(str, Len(str) - I) I =3D I + Len(rstr) rstr =3D "" Else I =3D I + 1 End If Wend conv_bin =3D str End Function Private Sub warn(str As String, abortq As Boolean) If DISPLAY_WARNINGS Then MsgBox str, vbOKOnly Or vbExclamation, = "Warning" warnings =3D warnings & COMMENT_PREFIX & " Warning: " & str & = Chr$(13) & Chr$(10) End Sub ------ =_NextPart_000_01BFF87E.9D3AAFE0--
php::bar PHP Wiki - Listenarchive