SQLite

Post

Posted
Rating:
#1 (In Topic #1236)
Avatar
Administrator
sholzy is in the usergroup ‘unknown’
When creating an SQLite table from within Gambas, I'd like to mark one of the columns UNIQUE (such as column name "name" in the example code below). I'm not having any success. Is there a way to do that? I know I could check what's being entered against existing rows to make sure the new data is unique.

Code

    If Not MGlobal.hDB.Tables.Exist("test") Then
        dbTable = MGlobal.hDB.Tables.Add("test")
        dbTable.Fields.Add("id", DB.Integer)
        dbTable.Fields.Add("name", DB.String, 50)
        dbTable.Fields.Add("url", DB.String, 255)
        dbTable.Fields.Add("show", DB.Integer)
        dbTable.PrimaryKey = ["id"]
        dbTable.Update()
    End If

sholzy
Gambas One Site Director

To report bugs in the Gambas IDE:
Official Gambas Bug Tracker
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
Hi  - I'd try creating the name field as an additional index.
You can read about it here on the SQLite site.

As a side note, you can create tables by storing the creation script as a file, reading it in and executing it as a SQL command.

This is an example that I've used to create a 'Category' table.

Code (gambas)

  1. CREATE TABLE 'Category' (
  2. 'Id' INTEGER PRIMARY KEY,
  3. 'CategoryName' VARCHAR(128),
  4. 'ParentId' INTEGER,
  5. 'CategoryUse' INTEGER,
  6. 'Information' TEXT,
  7. 'DateCreated' DATE,
  8. 'DateChanged' DATE);

I find this better than using the code creation method as you can edit an existing file and tweak for new fields and name and it takes far less time to create large table sets.

If you have an existing database with a lot of tables, you can create a backup of all your tables with this subroutine:

Code (gambas)

  1. Public Function CreateSQLiteScriptText(DropTableClause As Boolean, InName As String) As String
  2.  
  3.   Dim TmpLine As String
  4.   Dim KeyStr As String = ""
  5.   Dim LastLine As Integer
  6.   Dim TmpTable As Table
  7.   Dim TmpField As Field
  8.   Dim ReturnStr As String = ""
  9.  
  10.   TmpTable = $Con.Tables[InName]
  11.  
  12.   If DropTableClause Then TmpLine = "DROP TABLE IF EXISTS '" & InName & "';" & Gb.NewLine
  13.  
  14.   TmpLine &= "CREATE TABLE '" & InName & "' (" & Gb.NewLine
  15.  
  16.   LastLine = TmpTable.Fields.Count - 1
  17.  
  18.   For Each TmpField In TmpTable.Fields
  19.     KeyStr = ""
  20.     TmpLine &= "'" & TmpField.Name & "' "
  21.  
  22.     If CheckPrimaryKey(TmpTable, TmpField.Name) <> "" Then KeyStr = " PRIMARY KEY"
  23.  
  24.     If TmpField.Type = Gb.String Then
  25.       If TmpField.Length = 0 Then
  26.         TmpLine &= "TEXT"
  27.       Else
  28.         TmpLine &= GetFieldType(False, TmpField.Type) & "(" & TmpField.Length & ")" & KeyStr
  29.       Endif
  30.     Else
  31.       TmpLine &= GetFieldType(False, TmpField.Type) & KeyStr
  32.     Endif
  33.  
  34.     If LastLine > 0 Then
  35.       TmpLine &= "," & Gb.NewLine
  36.     Else
  37.       TmpLine &= ");" & Gb.NewLine
  38.     Endif
  39.     LastLine -= 1
  40.     ReturnStr &= TmpLine
  41.     TmpLine = ""
  42.   Next
  43.   Return ReturnStr
  44.   Message( Error.Text & Gb.CrLf &  Error.Backtrace)
  45.  


…and create them all with this one.

Code (gambas)

  1. Public Sub CreateTablesFromSuppliedScripts(InFolder As String, ByRef $InCon As Connection)
  2.  
  3.   Dim TmpFile As String
  4.   Dim FilesAry As String[]
  5.   Dim SQLCommand As String
  6.  
  7.   FilesAry = Dir(InFolder, "*.sql", Gb.File)
  8.   If FilesAry.Count > 0 Then
  9.     If Not $InCon.Opened Then $InCon.Open
  10.     For Each TmpFile In FilesAry
  11.       SQLCommand = File.Load(InFolder &/ TmpFile) ' Load the entire script from the file
  12.       $InCon.Exec(SQLCommand)          'Execute the Create Table script.
  13.     Next
  14.     $InCon.Close
  15.   Message( Error.Text & GB.CrLf & Error.Backtrace)
  16.  
  17.  

Cheers - Quin.
I code therefore I am
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Administrator
sholzy is in the usergroup ‘unknown’
Thanks Quin! I had created db in code (or with DB Browser for SQLite) for so long I had forgotten I can create a db using a script file in Gambas. After digging a little more I realized that I can create a unique index with code. Not exactly what I wanted, but just as good. After giving my use case more thought I decided I really don't even need a unique field since my db will contain just a few (maybe 6-8) records and it won't matter if any records are duplicates.

I'll file your information away somewhere for later use (or forgetting!  :lol: ).

sholzy
Gambas One Site Director

To report bugs in the Gambas IDE:
Official Gambas Bug Tracker
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
 No problems.
I do a lot of work with SQLite but nothing so complex or large that would require multiple indexes to speed up search functions.
Instead, I found that a bit of effort on good validation in the input form is a better solution and you can give you users a better constructed error message to fix the issue.

Cheers - Quin.
I code therefore I am
Online now: No Back to the top
1 guest and 0 members have just viewed this.