Hi,
I have the following code which SHOULD create a database, containing 1 Table called Boxes, and a column called SRRef.
Could someone please advise me how I add additional columns.
Do I simply place a comma and then the name of the new column, followed by its type?
Dim db As REALSQLdatabase
Dim f As FolderItem
Dim result As Boolean
f = New FolderItem("SR.db")
db = New REALSQLdatabase
db.DatabaseFile = f
result = db.CreateDatabaseFile
If db.Connect() Then
db.SQLExecute("CREATE TABLE Boxes(SRRef INTEGER PRIMARY KEY)")
db.Commit
Else
MsgBox("Database not created")
End If
Brian:
I am not worried about the contents, as this code is only run once in order to create a blank database.
I am just a bit unsure as to how to extend my code above in order to create an additional column.
It’s great place to learn about what these commands do.
I also use something called MySQLWorkBench which helps me create sql statements that I embed in my code.
Make your db property “As Database”, but instantiate it with a specific database class:
if dbtype = "sqlite" then
db = New SqliteDatabase
elseif dbtype = "mysql" then
db = New MySQLCommunityServer
end
Common functions, like SqlSelect and SqlError will work fine. To use db-specific methods, test for each type and cast accordingly.
if db ISA SqliteDatabase then
SqliteDatabase(db).DatabaseFile = GetFolderitem("somefile")
elseif db ISA MySQLCommunityServer then
MySqlCommunityServer(db).Host = "localhost"
MySqlCommunityServer(db).Username = "me"
MySqlCommunityServer(db).Password = "secret"
end
if db.Connect then
...
I figured I’d bring it up so that people were aware of the base class and how it can be used to make as much of the database code as generic as possible. It’s not an uncommon scenario to change database engines in the life cycle of the software, only to find there is a lot of work that could have been generic that was made specific.