Hello as a new user to xojo, i want to make a database for my work
I want to make database of candys in the store with their expiration date, so when i type in the current date, all candy that expere withing a month i want a list
ANy tips how to make it?
You will want to lay out your database using good design principles. Search YouTube for videos on “how to design a database using 3NF or Third Normal Form”
This tells you the kind of Xojo project you will need to build and it will help you understand the infrastructure you may or may not need. For example, if Web then will the application be hosted on an internal or external web server? Each of these has a variety of implications.
If it is Desktop which platforms will you be building for? Mac, Windows, or Linux or some combination?
Concurrent user count impacts what server “things” you will have to consider. For example, if you are planning on this being less than 10 users on a Web application then a single instance on an internal server would be fine. If more than 1 concurrent user on a Desktop then you will not be able to use SQLite and will need to use a database server of some kind like PostgreSQL, MariaDB, SQL Server, etc.
This will impact the needed windows and functionality. For example, if this is multiuser will there need to be a login screen so you know who is logged in and made what changes?
Based on what you have noted above I suspect many of the answers will be pretty simple and straight forward.
From a database perspective you will want a “product” table with a surrogate key that autoincrements or that you autoincrement in your code.
Your product table might look like this:
product_id integer Primary Key (unique across all rows)
product_nm varchar(50) NOT NULL
product_expiration_day_count integer NOT NULL ~ if your candy can expire at different rates then you would need to know that for each product. For example Skittles will expire in 30 days, but Hazelnut Truffles expire in 14 days.
update_timestamp ~ create and update timestamps are generally considered best practice to have, but you may not need these for now.
inventory_id Primary Key (autoincrement)
product_id integer NOT NULL ~ you need to have a foreign key from the product table into the inventory table so you know which product you are talking about
received_date date NOT NULL - so you know when to start the clock
on_hand_quantity integer NOT NULL so you know how much you have or how much you received
From here you then have a screen that takes the received_date and subtracts that from the current date and compares that to the product_expiration_day_count. If the count is equal or greater to this value then you know you need to do something.
If you are new to Xojo then I would start with a Desktop application and do the following:
Build out the SQLite database to suit your needs. Maybe you do it like above or not.
Build a window to input product records
Build a window to input inventory records
Build a window to show those candies that are about to expire or have expired.
Ask questions here as you go. This is a very helpful community who wants to see you succeed.
That should be plenty to do to start.
I just asked ChatGPT to write something quickly. I haven’t tested it. Below youfind what it came up with.
I would use a relational database instead. But that could be another question to ask ChatGPT.
Setting up the Database: Create a new Xojo project. Under the App object in the navigator, add a property named db of type SQLiteDatabase.
Connecting to the Database: Create a method to initialize the database. Call it ConnectToDB. The code should be something like:
db = New SQLiteDatabase
db.DatabaseFile = GetFolderItem("candyStore.sqlite")
If db.DatabaseFile.Exists Then
If Not db.Connect Then
MessageBox("Error connecting to database: " + db.ErrorMessage)
If Not db.TableExists("candies") Then
db.SQLExecute("CREATE TABLE candies (id INTEGER PRIMARY KEY, name TEXT, expirationDate DATE)")
Catch e As DatabaseException
MessageBox("DB Error: " + e.Message)
Adding Candy to the Database: Create another method AddCandy. This method will have two parameters: candyName As String and expDate As Date.
Dim sql As String = "INSERT INTO candies (name, expirationDate) VALUES (?, ?)"
db.SQLExecute(sql, candyName, expDate)
Catch e As DatabaseException
MessageBox("Error adding candy: " + e.Message)
Fetching Candies Expiring Within a Month: Create a method named GetExpiringCandies. This method will have one parameter: currentDate As Date.
Dim expiringList As RowSet
Dim expiringCandies() As String
Dim targetDate As New Date(currentDate.Year, currentDate.Month + 1, currentDate.Day)
expiringList = db.SQLSelect("SELECT name FROM candies WHERE expirationDate <= ?", targetDate)
For Each row As DatabaseRow In expiringList
Catch e As DatabaseException
MessageBox("Error fetching candies: " + e.Message)
Setting Up the User Interface:
In your window, add a TextField for entering the current date.
Add a Button to trigger the search.
Add a ListBox to display the expiring candies.For the Button’s Action event, fetch the date from the TextField, call GetExpiringCandies and populate the ListBox with the results.
You’ve received some excellent replies so far, so I’ll not repeat what they’ve said, but I did want to add that when you start Xojo, you will see the Project Chooser. At the bottom of the Project Chooser you’ll see Examples, and in there is a ton of code to get you started. Sometimes it’s easier to pull apart existing code and see how something works. Check out the Databases folder, and I’d guess you’ll want to look at the SQLite example. Xojo is perfect for this kind of app. Let us know how you get on.
There is no such method as “TableExists”. So you are bound to get an error if you use it.
It would help to know which OS you are using, and which version of Xojo. And, are you new to programming or just new to Xojo?
SQLite does not have a DATE datatype, but dates can be stored as TEXT items within an SQLite database.
I shall assume macOS until you tell me differently.
Lets assume you want this database to be on the Desktop. Then when you run your program, it should create the database if it does not already exist.
Var f as FolderItem, dbh as SQLiteDatabase, sql, myDate, mycandy as String
f = new FolderItem ("/Users/yourname/Desktop/candyStore", FolderItem.PathModes.Native)
dbh = new SQLiteDatabase
dbh.DatabaseFile = f
if (f.exists=False) then
dbh.CreateDatabase () // Create the database, then its tables
Catch err as DatabaseException
system.DebugLog ("Could not create database, code " + err.ErrorNumber.ToString + ": " + err.Message)
dbh.ExecuteSQL ("CREATE TABLE candies (id INTEGER PRIMARY KEY, name TEXT, expirationDate DATE)")
Now try adding a candy. You will have to work out how you want to make your dates. Note that the date you put into your database will be a String in Xojo, NOT a Date or DateTime . If you have such a string, like “2023-09-19 19:12:00” then you can do like this:
By the way, because it is very boring putting try/catch around every little thing, I tend to have wrapper methods which do the error handling and also the error reporting. For testing and until you build an application, something like like:
// For when we are doing a non-select SQL action. Returns False if an error occurred, otherwise True.
// Reports error to the debuglog (OK for debugging) or could be modified to write to a log file.
Var errnum As Integer, errmsg As String
dbh.ExecuteSQL (sql, args)
Return True // Worked fine, just return
Catch e as DatabaseException
app.dbErrorsCount = app.dbErrorsCount + 1
errnum = e.ErrorNumber
errmsg = e.Message
system.debuglog ("Error doing '" + sql + "', code " + errnum.ToString + ": " + errmsg)
You can declare this one as:
Method name: dbexec
Parameters: Extends dbh as sqlitedatabase, sql as string, ParamArray args as Variant
Return type: boolean
and call it thus:
Var sql as String, result as Boolean
sql = "INSERT INTO candies (name, expirationDate) VALUES (?, ?)"
result = dbh.dbexec (sql, mycandy, mydate)
if (result=False) then return