Maybe. But it’s more flexible to put your SQL Code into a Method which you could re-use.
To prevent SQLInjections and all kinds of weird stuff, you should use prepared statements. Xojo does most of the work for you. Here’s a simple example Method.
Private Function InsertNewCustomer() As Boolean
Try
If db.Connect Then
db.ExecuteSQL("INSERT INTO customers (custCompany, custAddress, custPostcode, custCheckbox, custDelCompany, custDelAddress, custDelPostcode, custName, custTel, custMob, custEmail, custNotes ) " + _
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", _
CustCompName.Text.ConvertEncoding(Encodings.ISOLatin1), _
CustCompAdd.Text.ConvertEncoding(Encodings.ISOLatin1), _
CustCompPostcode.Text.ConvertEncoding(Encodings.ISOLatin1), _
CustCompCheck1.Text.ConvertEncoding(Encodings.ISOLatin1), _
CustDelName.Text.ConvertEncoding(Encodings.ISOLatin1), _
CustDelAdd.Text.ConvertEncoding(Encodings.ISOLatin1), _
CustDelPostcode.Text.ConvertEncoding(Encodings.ISOLatin1), _
CustName.Text.ConvertEncoding(Encodings.ISOLatin1), _
custTel.Text.ConvertEncoding(Encodings.ISOLatin1), _
custMob.Text.ConvertEncoding(Encodings.ISOLatin1), _
custEmail.Text.ConvertEncoding(Encodings.ISOLatin1), _
custNotes.Text.ConvertEncoding(Encodings.ISOLatin1))
Return True
End If
Catch err As DatabaseException
// Do your DB Errorhandling here...
End Try
Return False
End Function
As you can see it’s a Method which returns a Boolean if the INSERT was successfull.
It also converts the encoding to the same encoding the Database is using.
And it catches Database issues.
BTW: You can mark the Code from “Private Function” to “End Function”, copy it and insert it using the right click context menu in the Xojo IDE Navigator, to add this Method to your Project.
Should I create the method to hold the ‘INSERT’ under the App in the navigator? And then when the user clicks my submit button i’d need to call for that method somehow?
This is such a mammoth task learning this stuff. But I’m determined to build this app.
I’ll read everything in the links. I just find it a bit difficult to retain information from reading
ClassA defines a public method, MyPublicMethod. That method can be called from any code.
MyPublicMethod is public and as such available from anywhere.
ClassA defines a protect method, MyProtectedMethod. Code in ClassA or ClassB can call that method.
MyProtectedMethod is a Method with the scope “protected” and as such only avail. within it’s namespace (ClassA can of course access it and ClassB can access it via it’s namespace, like this: ClassA.MyProtectedMethod. If there was a ClassC it could also access it using ClassA.MyProtectedMethod
ClassA defines a private method, MyPrivateMethod. Only code in ClassA may call the method.
MyPrivateMethod is private and as such only available for ClassA.
That’s only an example. Yes, SQLite uses UTF8 by default, but it could be anything. I just wanted to add that when working with databases, the encoding should always be provided as well.
there’s so many ways to achieve the same thing it would seem.
I’m just hoping for the simplest way to do this. A way that i can learn and repeat over and over again for all the windows/pages that will be on my app.
Your property is most likely not named “db” or not reachable (because of the scope). Replace “db” in my example with the name of your property.
Na… we answer your questions because we like to. We like to help and we all started as beginners like you do. We too learned from Forum, Documentations, Books, Classes, Videos, …
db = new SQLiteDatabase
db.DatabaseFile = new FolderItem("C:\Users\Burst01\Documents\SQLite\sqlite-dll-win64-x64-3410200\coatx.sqlite")
try
db.Connect
Catch e as DatabaseException
MessageBox e.Message
end try
The app does normally run, so i’m assuming it has been connecting to the db fine before now
Is it a property of the “App” Object or the “Window” Object or a Module, Class, … ?
I’m pretty sure the Window2.InsertNewCustomer Method can’t reach the db Object. Because it’s no property of Window2 and it then it needs to know the “path” to the db Property.
This Item does not exist = The Item does not exist or it is not known within the current namespace. Type “Int32” has no member named “Connect” = In our case this happens because Xojo auto-casts (it assumes) db to an Int32 and an Int32 has no Connect Event or Property.