von DPunch » Mo, 29.08.2011 17:08
Aloha
Beispielcode (auf die Schnelle zusammengebastelt, ohne Fehlerbehandlung etc.) - liest ein File ein, schreibt es in eine als (MEDIUM-)BLOB definierte MySQL-Tabellenspalte, liest das BLOB wieder aus und schreibt es zurück auf die Festplatte:
Code: Alles auswählen
'##### Anpassen
sDatabaseName = "meineDatenbank" 'Name, unter dem die Datenbank bei Base registriert ist
sTableName = "meineTabelle" 'Fragliche Tabelle
sColumName = "meineBLOBSpalte" 'Name der Spalte, in der das BLOB untergebracht ist/werden soll
sPrimaryKeyColumn = "ID" 'Name der Spalte, in der PK steht
sUser = "" 'Username
sPassword = "" 'Passwort
sInsertFileURL = "D:\meinBild.jpg" 'URL des einzulesenden Files
sOutputURL = "D:\meinBild2.jpg" 'URL, an die das File zurückgechrieben werden soll
'#####
sInsertFileURL = ConvertToURL(sInsertFileURL)
sOutputURL = ConvertToURL(sOutputURL)
oFileAccess = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
Dim aByteArray() as Byte
oDatabaseContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
oDataSource = oDatabaseContext.getByName(sDatabaseName)
oConnection = oDataSource.getConnection(sUser,sPassword)
nFileSize = oFileAccess.getSize(sInsertFileURL)
oInputStream = oFileAccess.openFileRead(sInsertFileURL)
oInputStream.readBytes(aByteArray,nFileSize)
oInputStream.closeInput
sSQL = "INSERT INTO `" & sTableName & "`(`" & sColumName & "`) VALUES(?)"
oPreparedStatement = oConnection.prepareStatement(sSQL)
oPreparedStatement.setBytes(1,aByteArray)
oPreparedStatement.executeUpdate
sSQL = "SELECT LAST_INSERT_ID() FROM `" & sTableName & "`"
oPreparedStatement = oConnection.prepareStatement(sSQL)
oResult = oPreparedStatement.executeQuery
oResult.Next
nLastID = oResult.getInt(1)
sSQL = "SELECT `" & sColumName & "` FROM `" & sTableName & "` WHERE `" & sPrimaryKeyColumn & "` = " & nLastID
oPreparedStatement = oConnection.prepareStatement(sSQL)
oResult = oPreparedStatement.executeQuery
oResult.Next
aByteArray = oResult.getBytes(1)
oOutputStream = oFileAccess.openFileWrite(sOutputURL)
oOutputStream.writeBytes(aByteArray)
oOutputStream.closeOutput
Aloha
Beispielcode (auf die Schnelle zusammengebastelt, ohne Fehlerbehandlung etc.) - liest ein File ein, schreibt es in eine als (MEDIUM-)BLOB definierte MySQL-Tabellenspalte, liest das BLOB wieder aus und schreibt es zurück auf die Festplatte:
[code] '##### Anpassen
sDatabaseName = "meineDatenbank" 'Name, unter dem die Datenbank bei Base registriert ist
sTableName = "meineTabelle" 'Fragliche Tabelle
sColumName = "meineBLOBSpalte" 'Name der Spalte, in der das BLOB untergebracht ist/werden soll
sPrimaryKeyColumn = "ID" 'Name der Spalte, in der PK steht
sUser = "" 'Username
sPassword = "" 'Passwort
sInsertFileURL = "D:\meinBild.jpg" 'URL des einzulesenden Files
sOutputURL = "D:\meinBild2.jpg" 'URL, an die das File zurückgechrieben werden soll
'#####
sInsertFileURL = ConvertToURL(sInsertFileURL)
sOutputURL = ConvertToURL(sOutputURL)
oFileAccess = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
Dim aByteArray() as Byte
oDatabaseContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
oDataSource = oDatabaseContext.getByName(sDatabaseName)
oConnection = oDataSource.getConnection(sUser,sPassword)
nFileSize = oFileAccess.getSize(sInsertFileURL)
oInputStream = oFileAccess.openFileRead(sInsertFileURL)
oInputStream.readBytes(aByteArray,nFileSize)
oInputStream.closeInput
sSQL = "INSERT INTO `" & sTableName & "`(`" & sColumName & "`) VALUES(?)"
oPreparedStatement = oConnection.prepareStatement(sSQL)
oPreparedStatement.setBytes(1,aByteArray)
oPreparedStatement.executeUpdate
sSQL = "SELECT LAST_INSERT_ID() FROM `" & sTableName & "`"
oPreparedStatement = oConnection.prepareStatement(sSQL)
oResult = oPreparedStatement.executeQuery
oResult.Next
nLastID = oResult.getInt(1)
sSQL = "SELECT `" & sColumName & "` FROM `" & sTableName & "` WHERE `" & sPrimaryKeyColumn & "` = " & nLastID
oPreparedStatement = oConnection.prepareStatement(sSQL)
oResult = oPreparedStatement.executeQuery
oResult.Next
aByteArray = oResult.getBytes(1)
oOutputStream = oFileAccess.openFileWrite(sOutputURL)
oOutputStream.writeBytes(aByteArray)
oOutputStream.closeOutput[/code]