Пытался разобраться с базой данных на PureBasic, примеры из
кодархива не запускались...
Нашел рабочий пример (тоже, чтоб запустить пришлось переделать)
Может кому пригодится...
Сделал для себя открытие:
думал, что сохраняется путь к изображениям,
оказывается они запихиваются в файл базы данных...
PB 4.41
Enumeration #Window_0 #Listview_0 #Button_0 #Button_1 #Button_2 #Button_3 #Button_4 #Image_0 #Text_0 #Text_1 #Text_2 #Text_4 #Text_5 #Text_6 #Text_7 #Text_8 #Text_9 #Text_10 #Text_11 #Text_12 #String_0 #String_1 #String_2 #String_3 #String_4 #String_5 #String_6 #String_7 #String_8 #String_9 #String_10 EndEnumeration UseODBCDatabase() Global glDBSQLite.l Global glListviewItemSelected.l = -1 Global glImageMemory.l UseJPEG2000ImageDecoder() UseJPEG2000ImageEncoder() UseJPEGImageDecoder() UseJPEGImageEncoder() UsePNGImageDecoder() UsePNGImageEncoder() Procedure Main_WindowOpen() If OpenWindow(#Window_0, 200, 200, 800, 390, "DVP - Управление контактами", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered ) ListViewGadget(#Listview_0, 10, 10, 300, 370) ButtonGadget(#Button_0, 320, 350, 080, 30, "Сброс") ButtonGadget(#Button_1, 400, 350, 100, 30, "Добавить") ButtonGadget(#Button_2, 500, 350, 100, 30, "Дата") ButtonGadget(#Button_3, 600, 350, 100, 30, "Удалить") ButtonGadget(#Button_4, 700, 350, 100, 30, "Обзор") TextGadget(#Text_0, 320, 20, 110, 20, "Фамилия :") TextGadget(#Text_1, 320, 50, 110, 20, "Имя : ") TextGadget(#Text_2, 320, 80, 110, 20, "Професия : ") TextGadget(#Text_4, 320, 110, 110, 20, "Должность :") TextGadget(#Text_5, 320, 140, 110, 20, "Адрес :") TextGadget(#Text_6, 320, 170, 110, 20, "Почтовый индекс :") TextGadget(#Text_7, 320, 200, 110, 20, "Ville :") TextGadget(#Text_8, 320, 230, 110, 20, "Страна :") TextGadget(#Text_9, 320, 260, 110, 20, "Телефон :") TextGadget(#Text_10, 320, 290, 110, 20, "Факс : ") TextGadget(#Text_11, 320, 320, 110, 20, "Mail :") TextGadget(#Text_12, 620, 20, 110, 20, "Фото :") StringGadget(#String_0, 450, 20, 150, 20, "") StringGadget(#String_1, 450, 50, 150, 20, "") StringGadget(#String_2, 450, 80, 150, 20, "") StringGadget(#String_3, 450, 110, 150, 20, "") StringGadget(#String_4, 450, 140, 150, 20, "") StringGadget(#String_5, 450, 170, 150, 20, "") StringGadget(#String_6, 450, 200, 150, 20, "") StringGadget(#String_7, 450, 230, 150, 20, "") StringGadget(#String_8, 450, 260, 150, 20, "") StringGadget(#String_9, 450, 290, 150, 20, "") StringGadget(#String_10, 450, 320, 150, 20, "") ImageGadget(#Image_0, 620, 50, 160, 240, 0, #PB_Image_Border) EndIf EndProcedure Procedure DB_Init() Protected plFile.l Protected psSQLRequest.s UseSQLiteDatabase() If FileSize("GestionContacts.sqlite") < 0 plFile = CreateFile(#PB_Any, "GestionContacts.sqlite") If plFile CloseFile(plFile) EndIf EndIf glDBSQLite = OpenDatabase(#PB_Any, "GestionContacts.sqlite", "", "", #PB_Database_SQLite) If glDBSQLite = 0 MessageRequester("DVP - Управление контактами", DatabaseError()) End ElseIf plFile psSQLRequest = "CREATE TABLE IF NOT EXISTS contacts (" psSQLRequest + "id_contact INTEGER PRIMARY KEY AUTOINCREMENT Not NULL, " psSQLRequest + "contact_nom TEXT Not NULL, " psSQLRequest + "contact_prenom TEXT Not NULL, " psSQLRequest + "contact_job TEXT, " psSQLRequest + "contact_company TEXT, " psSQLRequest + "contact_address TEXT, " psSQLRequest + "contact_postalcode TEXT, " psSQLRequest + "contact_city TEXT, " psSQLRequest + "contact_country TEXT, " psSQLRequest + "contact_phone TEXT, " psSQLRequest + "contact_fax TEXT, " psSQLRequest + "contact_mail TEXT, " psSQLRequest + "contact_photo BLOB, " psSQLRequest + "contact_photo_size INTEGER" psSQLRequest + ")" ; Debug psSQLRequest If DatabaseUpdate(glDBSQLite, psSQLRequest) = 0 MessageRequester("DVP - Управление контактами - DB_Init()", DatabaseError()) EndIf EndIf If DatabaseQuery(glDBSQLite, "SELECT * FROM contacts") <> 0 While NextDatabaseRow(glDBSQLite) AddGadgetItem(#Listview_0, CountGadgetItems(#Listview_0), GetDatabaseString(glDBSQLite, 1) + " " + GetDatabaseString(glDBSQLite, 2)) SetGadgetItemData(#Listview_0, CountGadgetItems(#Listview_0)-1, GetDatabaseLong(glDBSQLite, 0)) Wend FinishDatabaseQuery(glDBSQLite) EndIf EndProcedure Procedure DB_InsertContact() Protected psSQLRequest.s psSQLRequest = "INSERT INTO contacts" psSQLRequest + "(contact_nom, contact_prenom, contact_job, contact_company, contact_address, " psSQLRequest + "contact_postalcode, contact_city, contact_country, contact_phone, contact_fax, contact_mail, contact_photo, contact_photo_size)" psSQLRequest + "VALUES (" psSQLRequest + "'"+GetGadgetText(#String_0)+"'," psSQLRequest + "'"+GetGadgetText(#String_1)+"'," psSQLRequest + "'"+GetGadgetText(#String_2)+"'," psSQLRequest + "'"+GetGadgetText(#String_3)+"'," psSQLRequest + "'"+GetGadgetText(#String_4)+"'," psSQLRequest + "'"+GetGadgetText(#String_5)+"'," psSQLRequest + "'"+GetGadgetText(#String_6)+"'," psSQLRequest + "'"+GetGadgetText(#String_7)+"'," psSQLRequest + "'"+GetGadgetText(#String_8)+"'," psSQLRequest + "'"+GetGadgetText(#String_9)+"'," psSQLRequest + "'"+GetGadgetText(#String_10)+"'," psSQLRequest + "?," If glImageMemory psSQLRequest + Str(MemorySize(glImageMemory)) Else psSQLRequest + "0" EndIf psSQLRequest + ")" ;Debug psSQLRequest If glImageMemory SetDatabaseBlob(glDBSQLite, 0, glImageMemory, MemorySize(glImageMemory)) Else ReplaceString(psSQLRequest, "?", "'0'") EndIf If DatabaseUpdate(glDBSQLite, psSQLRequest) = 0 MessageRequester("DVP - Gestion de Contacts - DB_InsertContact()", DatabaseError()) EndIf ; Добавляет последние ajoutй элемент а база If DatabaseQuery(glDBSQLite, "SELECT max(id_contact), contact_nom, contact_prenom FROM contacts") <> 0 While NextDatabaseRow(glDBSQLite) AddGadgetItem(#Listview_0, CountGadgetItems(#Listview_0), GetDatabaseString(glDBSQLite, 1) + " " + GetDatabaseString(glDBSQLite, 2)) SetGadgetItemData(#Listview_0, CountGadgetItems(#Listview_0)-1, GetDatabaseLong(glDBSQLite, 0)) Wend FinishDatabaseQuery(glDBSQLite) Else MessageRequester("DVP - Gestion de Contacts - DB_InsertContact", DatabaseError()) EndIf EndProcedure Procedure DB_SelectContact(GadgetItem.l) Protected psSQLRequest.s Protected plFileImageSize.l Protected plFileImage.l psSQLRequest = "SELECT * FROM contacts WHERE id_contact="+Str(GetGadgetItemData(#Listview_0, GadgetItem)) ;Debug psSQLRequest If DatabaseQuery(glDBSQLite, psSQLRequest) <> 0 While NextDatabaseRow(glDBSQLite) SetGadgetText(#String_0, GetDatabaseString(glDBSQLite, 1)) SetGadgetText(#String_1, GetDatabaseString(glDBSQLite, 2)) SetGadgetText(#String_2, GetDatabaseString(glDBSQLite, 3)) SetGadgetText(#String_3, GetDatabaseString(glDBSQLite, 4)) SetGadgetText(#String_4, GetDatabaseString(glDBSQLite, 5)) SetGadgetText(#String_5, GetDatabaseString(glDBSQLite, 6)) SetGadgetText(#String_6, GetDatabaseString(glDBSQLite, 7)) SetGadgetText(#String_7, GetDatabaseString(glDBSQLite, 8)) SetGadgetText(#String_8, GetDatabaseString(glDBSQLite, 9)) SetGadgetText(#String_9, GetDatabaseString(glDBSQLite, 10)) SetGadgetText(#String_10, GetDatabaseString(glDBSQLite, 11)) plFileImageSize = GetDatabaseLong(glDBSQLite, 13) If plFileImageSize > 0 glImageMemory = AllocateMemory(plFileImageSize) GetDatabaseBlob(glDBSQLite, 12, glImageMemory, plFileImageSize) plFileImage = CatchImage(#PB_Any, glImageMemory, plFileImageSize) If plFileImage ResizeImage(plFileImage, 160, 240) SetGadgetState(#Image_0, ImageID(plFileImage)) FreeImage(plFileImage) EndIf Else SetGadgetState(#Image_0, 0) EndIf Wend FinishDatabaseQuery(glDBSQLite) Else MessageRequester("DVP - Gestion de Contacts - DB_SelectContact", DatabaseError()) EndIf EndProcedure Procedure DB_UpdateContact(GadgetItem.l) Protected psSQLRequest.s psSQLRequest = "UPDATE contacts SET " psSQLRequest + "contact_nom='"+GetGadgetText(#String_0)+"', " psSQLRequest + "contact_prenom='"+GetGadgetText(#String_1)+"', " psSQLRequest + "contact_job='"+GetGadgetText(#String_2)+"', " psSQLRequest + "contact_company='"+GetGadgetText(#String_3)+"', " psSQLRequest + "contact_address='"+GetGadgetText(#String_4)+"', " psSQLRequest + "contact_postalcode='"+GetGadgetText(#String_5)+"', " psSQLRequest + "contact_city='"+GetGadgetText(#String_6)+"', " psSQLRequest + "contact_country='"+GetGadgetText(#String_7)+"', " psSQLRequest + "contact_phone='"+GetGadgetText(#String_8)+"', " psSQLRequest + "contact_fax='"+GetGadgetText(#String_9)+"', " psSQLRequest + "contact_mail='"+GetGadgetText(#String_10)+"', " psSQLRequest + "contact_photo = ?," If glImageMemory psSQLRequest + "contact_photo_size = " + Str(MemorySize(glImageMemory)) Else psSQLRequest + "contact_photo_size = 0" EndIf psSQLRequest + " WHERE id_contact="+Str(GetGadgetItemData(#Listview_0, GadgetItem)) ;Debug psSQLRequest If glImageMemory SetDatabaseBlob(glDBSQLite, 0, glImageMemory, MemorySize(glImageMemory)) Else ReplaceString(psSQLRequest, "?", "'0'") EndIf If DatabaseUpdate(glDBSQLite, psSQLRequest) = 0 MessageRequester("DVP - Gestion de Contacts - DB_UpdateContact()", DatabaseError()) Else SetGadgetItemText(#Listview_0, GadgetItem, GetGadgetText(#String_0)+" "+GetGadgetText(#String_1)) EndIf EndProcedure Procedure DB_DeleteContact(GadgetItem.l) Protected psSQLRequest.s psSQLRequest = "DELETE FROM contacts WHERE id_contact="+Str(GetGadgetItemData(#Listview_0, GadgetItem)) ;Debug psSQLRequest If DatabaseUpdate(glDBSQLite, psSQLRequest) = 0 MessageRequester("DVP - Gestion de Contacts - DB_DeleteContact()", DatabaseError()) Else RemoveGadgetItem(#Listview_0, GadgetItem) EndIf EndProcedure Main_WindowOpen() DB_Init() Repeat glEvent = WaitWindowEvent() glGadget= EventGadget() glType = EventType() If glEvent = #PB_Event_Gadget Select glGadget Case #Listview_0 ;{ If GetGadgetState(#Listview_0) >= 0 And GetGadgetState(#Listview_0) <= CountGadgetItems(#Listview_0) If GetGadgetState(#Listview_0) <> glListviewItemSelected glListviewItemSelected = GetGadgetState(#Listview_0) DB_SelectContact(GetGadgetState(#Listview_0)) EndIf EndIf ;} Case #Button_0 ;{ Reset If glType = #PB_EventType_LeftClick Define.l dlInc For dlInc = #String_0 To #String_10 SetGadgetText(dlInc, "") Next SetGadgetState(#Image_0, 0) If glImageMemory FreeMemory(glImageMemory) glImageMemory = 0 EndIf glListviewItemSelected = -1 EndIf ;} Case #Button_1 ;{ Ajouter If GetGadgetText(#String_0) <> "" And GetGadgetText(#String_1) <> "" If glType = #PB_EventType_LeftClick DB_InsertContact() EndIf EndIf ;} Case #Button_2 ;{ Mettre а jour If GetGadgetState(#Listview_0) >= 0 And GetGadgetState(#Listview_0) <= CountGadgetItems(#Listview_0)-1 If glType = #PB_EventType_LeftClick DB_UpdateContact(GetGadgetState(#Listview_0)) EndIf EndIf ;} Case #Button_3 ;{ Supprimer If GetGadgetState(#Listview_0) >= 0 And GetGadgetState(#Listview_0) <= CountGadgetItems(#Listview_0)-1 If glType = #PB_EventType_LeftClick If MessageRequester("DVP - Gestion de Contacts", "Voulez vous vraiment supprimer le contact courant "+Chr(34)+GetGadgetItemText(#Listview_0, GetGadgetState(#Listview_0))+Chr(34)+" ?", #PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes DB_DeleteContact(GetGadgetState(#Listview_0)) EndIf EndIf EndIf ;} Case #Button_4 ;{ Parcourir Define.s psFileImage Define.l plFileImage ; Ouvre une boоte de dialogue standard pour choisir une image psFileImage = OpenFileRequester("Choisissez une image а charger", "", "Fichiers Images|*.png;*.bmp;*.jpg;*.jpeg|Tous les fichiers (*.*)|*.*", 0) If psFileImage ; Charge une image plFileImage = LoadImage(#PB_Any, psFileImage) If plFileImage ; Redimensionne l'image а la taille de l'ImageGadget ResizeImage(plFileImage, 160, 240) ; Change l'image affichйe par l' ImageGadget SetGadgetState(#Image_0, ImageID(plFileImage)) ; Libиre l'image FreeImage(plFileImage) ; Charge l'image dans la zone mйmoire globale dйdiйe 'glImageMemory' plFileImage = OpenFile(#PB_Any, psFileImage) If plFileImage glImageMemory = AllocateMemory(Lof(plFileImage)) If glImageMemory ReadData(plFileImage, glImageMemory, Lof(plFileImage)) EndIf CloseFile(plFileImage) EndIf EndIf EndIf ;} EndSelect EndIf Until glEvent = #PB_Event_CloseWindow End
архив тот-же код и файл б.д. 222КБ http://mirashic.narod.ru/fail/database.rar
Отредактировано mirashic (12.10.2010 15:14:23)