2011-03-31 73 views
0

我使用SQLite(3.7.5版本),在http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBCsqlite的setBytes()不工作的BLOB數據類型在Windows

提供sqlite的jdbc驅動我問了一個幾乎相同的問題,幾個月前,在xerial論壇(HTTP ://groups.google.com/group/xerial/browse_thread/thread/ee19bd855e282f9c),但從來沒有得到任何迴應。

當我自己構建共享庫sqlite.dll/libsqlite.so時,我的示例在Linux上運行正常(opensuse 64位),但在Windows XP Professional 32位上無法正常工作。

但是,如果我使用sqlite.org網站提供的共享庫(http://sqlite.org/sqlite-dll-win32-x86-3070500.zip)或與jdbc驅動程序捆綁在一起的共享庫(http://www.xerial.org/maven/repository/artifact/org/xerial/sqlite-jdbc/3.7.2/sqlite-jdbc-3.7.2.jar),它可以在Linux和Windows上正常工作。

因此,我猜測我沒有正確構建sqlite庫。我正在使用Microsoft Platform SDK的cygwin環境來構建sqlite。我正在使用以下一組命令在Windows XP 32位上創建sqlite庫。

mycl -32 -O2/d 「NDEBUG」/ MD/d 「_WIN32」/ d 「_WINDOWS」/ d 「_MBCS」/ d 「_USRDLL」/ d 「SQLITE_ENABLE_COLUMN_METADATA」/ d 「SQLITE_ENABLE_FTS3」/ d「SQLITE_THREADSAFE = 1「-c NativeDB.c -o NativeDB.o

mycl -32 -O2/D」NDEBUG「/ MD/D」_WIN32「/ D」_WINDOWS「/ D」_MBCS「/ D」_USRDLL「/ d 「SQLITE_ENABLE_COLUMN_METADATA」/ d 「SQLITE_ENABLE_FTS3」/ d 「SQLITE_THREADSAFE = 1」 -c sqlite3.c -o sqlite3.o

myLink的-32/DLL /libpath:../lib/Win32 /out:sqlite.dll NativeDB.o sqlite3.o gdi32.lib vfw32.lib user32.lib comdlg32.lib comctl32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib wbemuuid.lib netapi32.lib ws2_32.lib kernel32.lib

mymt -32 /清單sqlite.dll.manifest /outputresource:sqlite.dll';#2'

(mycl,myLink的,和mymt是圍繞原始cl.exe時,LINK.EXE,和mt包裝.exe轉換命令行參數。他們與我一起構建的許多其他項目都能很好地工作)。

我也創建了一個SSCE來演示這個問題。

package org.sqlite; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

public class MainDriver { 


public static void main(String[] args) { 
    new MainDriver(); 
} 


public MainDriver() { 

    //Buffers to read and write 
    byte[] writeBuffer = new byte[10]; 
    byte[] readBuffer = null; 
    for (int i = 1; i < 10; i++) { 
     writeBuffer[i] = (byte)i; 
    } 

    //Database objects 
    Connection conn = null; 
    Statement stat = null; 
    PreparedStatement prep = null; 

    //Load the database driver 
    try { 
     System.loadLibrary("sqlite"); 
     Class.forName("org.sqlite.JDBC"); 
    } catch (Exception e) { 
     System.err.println("Could not load sqlite library or instantiate the database driver."); 
     System.err.println(e); 
     e.printStackTrace(); 
     return; 
    } 

    //Open a connection to the database 
    try { 
     conn = DriverManager.getConnection("jdbc:sqlite:" + "file.db"); 
    } catch (SQLException e) { 
     System.err.println("Could not open a connection to the database with name file.db"); 
     System.err.println(e); 
     e.printStackTrace(); 
     return; 
    } 

    //Create a table 
    try { 
     stat = conn.createStatement(); 
     stat.execute("CREATE TABLE TEST (model BLOB NOT NULL)"); 
     stat.close(); 
    } catch (SQLException e) { 
     System.err.println("The table could not be created."); 
     System.err.println(e); 
     e.printStackTrace(); 
     return; 
    } 

    //Write buffer into the database 
    try { 
     conn.setAutoCommit(false); 
     prep = conn.prepareStatement("INSERT INTO TEST (model) VALUES(?)"); 
     prep.setBytes(1, writeBuffer); 
     prep.addBatch(); 
     prep.executeBatch(); 
     conn.setAutoCommit(true); 
     prep.close(); 
    } catch (SQLException e) { 
     System.err.println("The buffer could not be written to the database."); 
     System.err.println(e); 
     e.printStackTrace(); 
     return; 
    } 

    //Read buffer from the database 
    try { 
     stat = conn.createStatement(); 
     ResultSet rs = stat.executeQuery("SELECT * FROM TEST"); 
     readBuffer = rs.getBytes(1); 
     rs.close(); 
     stat.close(); 
    } catch (SQLException e) { 
     System.err.println("The buffer could not be read"); 
     System.err.println(e); 
     e.printStackTrace(); 
    } 

    //Close the database 
    try { 
     conn.close(); 
    } catch (SQLException e) { 
     System.err.println("Database could not be closed"); 
     System.err.println(e); 
     e.printStackTrace(); 
    } 

    //Print the buffers 
    System.out.print("Write buffer = "); 
    for (int i = 0; i < writeBuffer.length; i++) { 
     System.out.print(writeBuffer[i]); 
    } 
    System.out.println(); 
    System.out.print("Read buffer = "); 
    for (int i = 0; i < readBuffer.length; i++) { 
     System.out.print(readBuffer[i]); 
    } 
    System.out.println(); 

    //Check the md5sum 
    try { 
     java.security.MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); 
     byte[] md5sum = null; 
     java.math.BigInteger bigInt = null; 

     //Write buffer 
     digest.reset(); 
     digest.update(writeBuffer); 
     md5sum = digest.digest(); 
     bigInt = new java.math.BigInteger(1, md5sum); 
     System.out.println("MD5 checksum of write buffer = " + bigInt.toString(16)); 

     //Read buffer 
     digest.reset(); 
     digest.update(readBuffer); 
     md5sum = digest.digest(); 
     bigInt = new java.math.BigInteger(1, md5sum); 
     System.out.println("MD5 checksum of read buffer = " + bigInt.toString(16)); 
    } catch (Exception e) { 
     System.err.println("MD5 checksum not available"); 
     return; 
    } 
} 

}

我也試圖建立與源碼ICU的Unicode庫(4.4.2版本)。我正在使用以下命令來構建帶有unicode支持的sqlite。

cl.exe時-32 -O2/d 「NDEBUG」/ MD/d 「_WIN32」/ d 「_WINDOWS」/ d 「_MBCS」/ d 「_USRDLL」/ d 「SQLITE_ENABLE_COLUMN_METADATA」/ d 「SQLITE_ENABLE_FTS3」/ d 「SQLITE_THREADSAFE = 1」/ D「SQLITE_ENABLE_ICU」-I ../ external/icu/win32/include -I ../ include -c NativeDB.c -o NativeDB.o

cl.exe -32 -O2/D「NDEBUG」/ MD/D「_WIN32」/ D「_WINDOWS」/ D「_MBCS」/ D「_USRDLL」/ D「SQLITE_ENABLE_COLUMN_METADATA」/ D「SQLITE_ENABLE_FTS3」/ D「SQLITE_THREADSAFE = 1」/ D「SQLITE_ENABLE_ICU」 ../external/icu/win32/include -I ../ include -c sqlite3.c -o sqlite3.o

link.exe -32/DLL /libpath:../external/icu/win32/lib /out:sqlite.dll NativeDB.o sqlite3.o icuuc.lib icuin.lib gdi3 2.lib vfw32.lib user32.lib comdlg32.lib comctl32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib wbemuuid.lib netapi32.lib ws2_32.lib kernel32。LIB

mt.exe -32 /清單sqlite.dll.manifest /outputresource:sqlite.dll';#2'

樓內有/無Unicode有沒有效果。我仍然無法解決問題。我將非常感謝任何幫助或指向可能的解決方案/解決方法。

回答

0

我終於解決了這個問題。罪魁禍首是由Visual Studio 6自動添加的「NDEBUG」標誌。以下一組命令正確地構建了sqlite庫(構建環境是Microsoft Windows SDK v6.1 - 發行年份2008)。

cl.exe時/ O2/GL/d 「WIN32」/ d 「_WINDLL」/ d 「_UNICODE」/ d 「UNICODE」/ MD/W3/C/Wp64/TC/d 「SQLITE_ENABLE_COLUMN_METADATA」/ d「SQLITE_ENABLE_FTS3 「/ D」SQLITE_THREADSAFE = 1「/ D」SQLITE_ENABLE_ICU「/ I」../external/icu/Win32/include「sqlite3.c

cl.exe/O2/GL/D」WIN32「/ D」_DLL 「/ D」_WINDLL「/ D」_UNICODE「/ D」UNICODE「/ MD/W3/c/Wp64/TC/D」SQLITE_ENABLE_COLUMN_METADATA「/ D」SQLITE_ENABLE_FTS3「/ D」SQLITE_THREADSAFE = 1「/ D」SQLITE_ENABLE_ICU「 c

link.exe/INCREMENTAL:NO/DLL/SUBSYSTEM:CONSOLE/OPT:REF/OPT:ICF/LTCG/MACHINE:X86 /LIBPATH:"../external/icu/Win32/lib「」/ out :sqlite.dll「NativeDB.obj sqlite3.obj icuuc.lib icui n.lib KERNEL32.LIB USER32.LIB GDI32.LIB winspool.lib comdlg32.lib advapi32.lib SHELL32.LIB ole32.lib oleaut32.lib UUID.LIB

mt.exe /清單sqlite.dll.manifest/outputresource: sqlite.dll';#2'

我發佈的命令,以防其他人可能會遇到同樣的問題。