简单的例子,sqlite的东西都忘光了,哎……
;Purebasic+sqlite 使用学习 ;全局变量声明 Global DbFile.s DbFile = "" Enumeration #Button_0 #Button_1 #Button_2 #Button_3 EndEnumeration ;函数声明 Declare CreateDb() Declare InsertDb() Declare SearchDb() Declare DeleteDb() Declare Creatdb_Click() Declare Insertdb_Click() Declare Searchdb_Click() Declare Deletedb_Click() ;程序初始化 UsesqliteDatabase() ;窗口 If OpenWindow(0,500,200,195,280,"PureBasic Window",#PB_Window_SystemMenu) ButtonGadget(#Button_0,50,20,90,40,"生成数据文件") ButtonGadget(#Button_1,80,"插入数据") ButtonGadget(#Button_2,150,"查询数据") ButtonGadget(#Button_3,220,"删除数据") Repeat Event = WaitWindowEvent() Select Event Case #PB_Event_Gadget Select EventGadget() Case #Button_0 Creatdb_Click() Case #Button_1 Insertdb_Click() Case #Button_2 Searchdb_Click() Case #Button_3 Deletedb_Click() EndSelect Case #PB_Event_CloseWindow Quit = 1 EndSelect Until Quit = 1 EndIf End ;函数实现 Procedure CheckDb(Database.l,Query.s) re = DatabaseUpdate(Database,Query) If re = 0 Debug DatabaseError() EndIf ProcedureReturn re EndProcedure Procedure CreateDb() FileName.s = SaveFileRequester("保存数据文件到...","MyDb.sdb","*.sdb|*.sdb",0) If FileName If CreateFile(0,FileName) CloseFile(0) EndIf If OpenDatabase(0,FileName,"",#PB_Database_sqlite) CheckDb(0,"CREATE TABLE docs (ID INT,ParentId INT,Title CHAR(140),Texts TEXT,CreateTime INT,LastModifyTime INT)") DbFile = FileName CloseDatabase(0) EndIf EndIf EndProcedure Procedure InsertDb() DbFile = "D:/MyDb.sdb" If OpenDatabase(0,DbFile,#PB_Database_sqlite) re = CheckDb(0,"INSERT INTO docs (ID,ParentId,Title,Texts,CreateTime,LastModifyTime) VALUES (1,'又想老爸,又想研子','真奇怪,为什么会想研子呢?',12354,12365)") If re = 0 ProcedureReturn EndIf Debug "插入数据成功!" CloseDatabase(0) Else CreateDb() Insertdb_Click() EndIf EndProcedure Procedure SearchDb() DbFile = "D:/MyDb.sdb" If OpenDatabase(0,#PB_Database_sqlite) If DatabaseQuery(0,"SELECT * FROM docs WHERE ID < 4") While NextDatabaseRow(0) Debug GetDatabaseString(0,2) Wend FinishDatabaseQuery(0) EndIf CloseDatabase(0) Else CreateDb() InsertDb() Searchdb_Click() EndIf EndProcedure Procedure DeleteDb() DbFile = "D:/MyDb.sdb" If OpenDatabase(0,"DELETE FROM docs WHERE ID = 1") If re = 0 ProcedureReturn EndIf Debug "删除数据成功!" CloseDatabase(0) Else CreateDb() InsertDb() Deletedb_Click() EndIf EndProcedure Procedure Creatdb_Click() CreateDb() EndProcedure Procedure Insertdb_Click() InsertDb() EndProcedure Procedure Searchdb_Click() SearchDb() EndProcedure Procedure Deletedb_Click() DeleteDb() EndProcedure
不管学编程还是其他,都需要不断地练习,做到真知在心,熟习在手,那么遇到问题了就马上心中有底,能够有一个解决问题的思路,才能更好的解决问题!