注册 登录
编程论坛 VFP论坛

vfp 连接sqlite数据库教程

sam_jiang 发布于 2025-02-16 16:14, 1846 次点击
网上说sqlite是个轻量级的数据库,无服务器,零配置,事务性的sql数据库引擎,所以有必要了解一下,vfp环境下如何使用。

带着这个目的,摸索安装,配置,把过程记录下来,分享给大家。

首先到这个网址下载sqlite3,https://,里面包含了odbc驱动和sqlite。压缩文件中有2个安装文件,sqliteodbc.exe 和sqlitedbc_w64.exe,应该是对应于不同的windows系统的。我是window7系统,vfp9是32位的,就选择安装了sqliteodbc,各位可以自行决定如何安装。

然后安装,我安装在e盘根目录下。

接着在odbc管理器里设置数据源,以备vfp调用,这里要小费周章了。因为是win7系统,直接在开始菜单里搜索出来的odbc管理器是不能找到刚安装的驱动的!它是64位的!如果是xp系统的,那就没有问题。

正确的odbc管理器在 C:\Windows\SysWOW64 目录下,名为odbcad32.exe,双击它,就跳出配置的界面,这时点击添加时就可以从列表中找到刚安装的驱动了。

拉到最下,找到sqlite3 odbc driver,选择后,按完成,跳出设置界面,在data source name 输入一个名称,我命名为mysqlite,确定后退出。

这样就完成了odbc的安装,现在就可以在vfp中使用它了。

由于sqlite没有用户名和密码要求,所以使用非常简单!

vfp代码:

nconncect=sqlconnect("mysqlite")
if nconnect>0
    messagebox("Have connected to Sqlite!")
else
    messagebox("Can't connect to Sqlite!")
endif
...

希望能帮到大家
22 回复
#2
schtg2025-02-17 06:24
谢谢!
#3
sych2025-02-17 08:29
非常感谢,星星之火可以燎原,大家都来贡献自己的一份微薄之力,愿vfp还能走的更远些
#4
laxjyj2025-02-17 10:32
版本太老
只有本站会员才能查看附件,请 登录

我发一个0.9995的上来
只有本站会员才能查看附件,请 登录


[此贴子已经被作者于2025-2-17 10:36编辑过]

#5
cxzbzgz2025-02-17 10:58
谢谢分享!
#6
wcx_cc2025-02-17 14:06
感谢分享!但是,有些sqlite表,内部数据格式需要转换。否则是乱码。sqlite 也可以直接用代码连接(安装驱动后)
nhand=SQLSTRINGCONNECT("driver={SQLite3 ODBC Driver};database='data.sqlite';UID=;PWD=;",.T.)
=SQLSETPROP(0,"DispLogin",3)




[此贴子已经被作者于2025-2-17 14:32编辑过]

#7
wxzd1232025-02-17 18:35
好,继续
#8
sam_jiang2025-02-22 17:34
回复 6楼 wcx_cc
对于sqlite数据库,用sqlstringconnect比sqlconnect更好!
#9
nhdjh2025-02-25 09:07
加油
#10
yxmwj4192025-04-24 10:41
回复 4楼 laxjyj
#11
sych2025-04-25 08:49
网上说sqlite小巧、高效、迅速,我怎么使用非常卡顿,是不是数据量大的缘故?

#12
sam_jiang2025-04-25 13:21
回复 11楼 sych
那是指sqlite本身而言,它是文件型数据库,如果你的数据量很大,建议还是用mysql
#13
sych2025-04-25 15:21
好的,找到个KTV歌曲库文件,sqlite数据库,感觉查询比较慢
#14
厨师王德榜2025-04-28 11:10
不错的示范,但是 还没有达到“零配置”。
举例来说,假设你写的程序发到别人电脑上用,你是不是还要帮他调整ODBC ? 如果这一步调整ODBC不需要,才叫“零配置”。
理想中的“零配置”,我是这样理解的:
就是在你的代码中,通过直接调用Sqlite3.dll中已有的api,让代码直接可运行,
这样放到别人电脑上,只要你文件打包时,文件夹中有Sqlite3.dll,别人电脑就可以直接运行了。
#15
nbwww2025-04-28 12:27
以下是引用厨师王德榜在2025-4-28 11:10:59的发言:

不错的示范,但是 还没有达到“零配置”。
举例来说,假设你写的程序发到别人电脑上用,你是不是还要帮他调整ODBC ? 如果这一步调整ODBC不需要,才叫“零配置”。
理想中的“零配置”,我是这样理解的:
就是在你的代码中,通过直接调用Sqlite3.dll中已有的api,让代码直接可运行,
这样放到别人电脑上,只要你文件打包时,文件夹中有Sqlite3.dll,别人电脑就可以直接运行了。
这是正确的打开方式
#16
sych2025-04-28 17:07
转发
*======= vfp api访问sqlite例子  ======
*

*/*
** CAPI3REF: Flags For File Open Operations
**
** These bit values are intended for use in the
** 3rd parameter to the [sqlite3_open_v2()] interface and
** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
*/
#define SQLITE_OPEN_READONLY         0x00000001  &&/* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_READWRITE        0x00000002  &&/* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_CREATE           0x00000004  &&/* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_DELETEONCLOSE    0x00000008  &&/* VFS only */
#define SQLITE_OPEN_EXCLUSIVE        0x00000010  &&/* VFS only */
#define SQLITE_OPEN_AUTOPROXY        0x00000020  &&/* VFS only */
#define SQLITE_OPEN_URI              0x00000040  &&/* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_MAIN_DB          0x00000100  &&/* VFS only */
#define SQLITE_OPEN_TEMP_DB          0x00000200  &&/* VFS only */
#define SQLITE_OPEN_TRANSIENT_DB     0x00000400  &&/* VFS only */
#define SQLITE_OPEN_MAIN_JOURNAL     0x00000800  &&/* VFS only */
#define SQLITE_OPEN_TEMP_JOURNAL     0x00001000  &&/* VFS only */
#define SQLITE_OPEN_SUBJOURNAL       0x00002000  &&/* VFS only */
#define SQLITE_OPEN_MASTER_JOURNAL   0x00004000  &&/* VFS only */
#define SQLITE_OPEN_NOMUTEX          0x00008000  &&/* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_FULLMUTEX        0x00010000  &&/* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_SHAREDCACHE      0x00020000  &&/* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_PRIVATECACHE     0x00040000  &&/* Ok for sqlite3_open_v2() */
#define SQLITE_OPEN_WAL              0x00080000  &&/* VFS only */



*/*
** CAPI3REF: Result Codes
** KEYWORDS: SQLITE_OK {error code} {error codes}
** KEYWORDS: {result code} {result codes}
**
** Many SQLite functions return an integer result code from the set shown
** here in order to indicate success or failure.
**
** New error codes may be added in future versions of SQLite.
**
** See also: [SQLITE_IOERR_READ | extended result codes],
** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
*/
#define SQLITE_OK           0   &&/* Successful result */
*/* beginning-of-error-codes */
#define SQLITE_ERROR        1   &&/* SQL error or missing database */
#define SQLITE_INTERNAL     2   &&/* Internal logic error in SQLite */
#define SQLITE_PERM         3   &&/* Access permission denied */
#define SQLITE_ABORT        4   &&/* Callback routine requested an abort */
#define SQLITE_BUSY         5   &&/* The database file is locked */
#define SQLITE_LOCKED       6   &&/* A table in the database is locked */
#define SQLITE_NOMEM        7   &&/* A malloc() failed */
#define SQLITE_READONLY     8   &&/* Attempt to write a readonly database */
#define SQLITE_INTERRUPT    9   &&/* Operation terminated by sqlite3_interrupt()*/
#define SQLITE_IOERR       10   &&/* Some kind of disk I/O error occurred */
#define SQLITE_CORRUPT     11   &&/* The database disk image is malformed */
#define SQLITE_NOTFOUND    12   &&/* Unknown opcode in sqlite3_file_control() */
#define SQLITE_FULL        13   &&/* Insertion failed because database is full */
#define SQLITE_CANTOPEN    14   &&/* Unable to open the database file */
#define SQLITE_PROTOCOL    15   &&/* Database lock protocol error */
#define SQLITE_EMPTY       16   &&/* Database is empty */
#define SQLITE_SCHEMA      17   &&/* The database schema changed */
#define SQLITE_TOOBIG      18   &&/* String or BLOB exceeds size limit */
#define SQLITE_CONSTRAINT  19   &&/* Abort due to constraint violation */
#define SQLITE_MISMATCH    20   &&/* Data type mismatch */
#define SQLITE_MISUSE      21   &&/* Library used incorrectly */
#define SQLITE_NOLFS       22   &&/* Uses OS features not supported on host */
#define SQLITE_AUTH        23   &&/* Authorization denied */
#define SQLITE_FORMAT      24   &&/* Auxiliary database format error */
#define SQLITE_RANGE       25   &&/* 2nd parameter to sqlite3_bind out of range */
#define SQLITE_NOTADB      26   &&/* File opened that is not a database file */
#define SQLITE_ROW         100  &&/* sqlite3_step() has another row ready */
#define SQLITE_DONE        101  &&/* sqlite3_step() has finished executing */
*/* end-of-error-codes */

#define SQLITE_INTEGER  1
#define SQLITE_FLOAT    2
#define SQLITE_BLOB     4
#define SQLITE_NULL     5
*#ifdef SQLITE_TEXT
*# undef SQLITE_TEXT
*#else
#define SQLITE_TEXT     3
*#endif
#define SQLITE3_TEXT     3



*SET PATH TO "I:\SQLite\sqlite_api"
*SET defaul TO "I:\SQLite\sqlite_api"
DECLARE sqlite3_initialize  IN sqlite3.dll
DECLARE integer sqlite3_open_v2  IN sqlite3.dll   string dbfn, integer @hd, integer bz,integer temp
DECLARE sqlite3_close IN sqlite3.dll      integer dbhand
DECLARE sqlite3_shutdown IN sqlite3.dll

DECLARE integer sqlite3_prepare_v2  IN sqlite3.dll integer dbhand,string sqlchar, integer clen, integer @stmt,integer temp
DECLARE integer sqlite3_step  IN sqlite3.dll integer stmt
DECLARE sqlite3_finalize  IN sqlite3.dll integer stmt

DECLARE integer  sqlite3_column_count IN sqlite3.dll integer stmt
DECLARE integer  sqlite3_column_type  IN sqlite3.dll integer stmt,integer culm

DECLARE integer sqlite3_column_int IN sqlite3.dll integer stmt,integer culm
DECLARE integer sqlite3_column_double IN sqlite3.dll integer stmt,integer culm
DECLARE string sqlite3_column_text  IN sqlite3.dll integer stmt,integer culm


=sqlite3_initialize()
LOCAL dbhd,dbfile,rc,stmthd,jg,culs,js,columtype
dbhd=0
stmthd=0
dbfile="song.db"
rc = sqlite3_open_v2( dbfile, @dbhd,2,0 ) &&SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL )
?rc
IF rc<>0
    ?'open err'
ELSE
    TRY
        ?'open db ok'
*        cx="select * from v_classtype"+CHR(0)
        cx="select * from song"+CHR(0)

        rc=sqlite3_prepare_v2(dbhd,cx,len(cx),@stmthd,0)  &&查询准备
        IF rc = 0
            hs=0
            DIMENSION rsult(100,1)
            DO WHILE .t.
                jg=sqlite3_step(stmthd)   &&执行查询获得一行结果
*         ?jg,SQLITE_ROW
                IF jg = SQLITE_ROW THEN
                    culs=sqlite3_column_count(stmthd) &&获得列数
                    hs=hs+1
                    DIMENSION rsult(hs,culs)
                    FOR js=1 TO culs STEP 1
                        columtype=sqlite3_column_type(stmthd,js)  &&获得列类型
                        DO CASE
                        CASE  columtype = SQLITE_INTEGER
                            vv=sqlite3_column_int(stmthd,js)

                        CASE  columtype = SQLITE_FLOAT
                            vv=sqlite3_column_double(stmthd,js)

                        CASE  columtype = SQLITE_TEXT
                            vv=STRCONV( sqlite3_column_text(stmthd,js),11)  && UTF-8 CONVERT
                        CASE  columtype = SQLITE_NULL
                            vv="NULL"
                        CASE  columtype = SQLITE_BLOB

                        OTHERWISE
                            ?"columtype err"
                        ENDCASE
                        rsult(hs,js)=vv
                    NEXT
                    DO CASE
                    CASE culs=5
                        ?rsult(hs,1),rsult(hs,2),rsult(hs,3),rsult(hs,4),rsult(hs,5),culs &&根据列数目显示所有列内容
                    CASE culs=4
                        ?rsult(hs,1),rsult(hs,2),rsult(hs,3),rsult(hs,4),culs
                    CASE culs=3
                        ?rsult(hs,1),rsult(hs,2),rsult(hs,3),culs
                    CASE culs=2
                        ?rsult(hs,1),rsult(hs,2),culs
                    CASE culs=1
                        ?rsult(hs,1),culs
                    ENDCASE
exit
                ELSE
                    exit
                ENDIF
            ENDDO
            sqlite3_finalize(stmthd)  &&销毁prepare防止内存泄漏
        else
            ?"a err "
&&  如果用sqlite3_exec 出错则需要用 sqlite3_free 销毁对象防止内存泄漏
        endif
    CATCH
        ?"except",MESSAGE(1),MESSAGE()
    FINALLY

        =sqlite3_close(dbhd)   &&关闭连接
    ENDTRY
ENDIF

=sqlite3_shutdown()
RETURN
#17
schtg2025-04-28 19:03
回复 16楼 sych
学习啦,谢谢!
#18
nbwww2025-04-28 19:29
回复 16楼 sych
你用的是哪个版本的sqlite3.dll?能上传一个吗?
#19
sych2025-04-29 08:50
只有本站会员才能查看附件,请 登录
#20
nbwww2025-04-29 18:06
以下是引用sych在2025-4-29 08:50:58的发言:
#21
girlsfriend2025-05-13 09:58
学习了
#22
hsfisher2025-05-13 10:21
谢谢分享
#23
hsfisher2025-05-22 10:40
谢谢分享
1