INSERT sqlite

Howdy folks,
I’m getting to grips with xojo day by day I learn a little bit more. I’m ready to insert some data into the database.

I have a simple customer window with standard fields for name, address telephone number etc etc.

Do i create an even handler for the clicking of my button and put some sql in there?
also could someone help me with this sql please?

thanks
zac

INSERT INTO customers ( custCompany, custAddress, custPostcode, custCheckbox, custDelCompany, custDelAddress, custDelPostcode, custName, custTel, custMob, custEmail, custNotes )
VALUES
"
( '" + CustCompName.TEXT + "', '" + CustCompAdd.TEXT + "', '" + CustCompPostcode.TEXT + "', '" + CustCompCheck1.TEXT + "', '" + CustDelName.TEXT + "', '" + CustDelAdd.TEXT + "', '" + CustDelPostcode.TEXT + "', '" + CustName.TEXT + "', '" + custTel.TEXT + "', '" + custMob.TEXT + "', '" + custEmail.TEXT + "', '" + custNotes.TEXT + "' );

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.

Thank you so much for such a detailed response.

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?

1 Like

Yes, or you can add a module (call it mDatabase) and add the Method there…

NB: you may want to read:

https://documentation.xojo.com/topics/databases/database_basics_for_beginners.html#database-basics-for-beginners

No. Try always to keep the scope as small as possible.

If you are sure you need the Method only within a single Window, make it a Method of that Window and set the Scope to private.

More about the Scope of an Object:

  • ClassA defines a public method, MyPublicMethod. That method can be called from any code.
  • ClassA defines a protect method, MyProtectedMethod. Code in ClassA or ClassB can call that method.
  • ClassA defines a private method, MyPrivateMethod. Only code in ClassA may call the method.

Source:
https://documentation.xojo.com/getting_started/object-oriented_programming/oop_design_concepts.html#getting-started-object-oriented-programming-oop-design-concepts-how-scope-affects-access

ok thank you. I understand this.

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 :frowning:

If you have a second Monitor, keep the page open;
If you have a printer, just print it and keep the pages handly.

With (hard) time, the memory will retain the information.

1 Like

is this a typo? should it say class A B C ?

SQLite uses UTF8 encoding by default, not sure why you convert all the values to ISOLatin1.

1 Like

No, it’s correct afaik :slight_smile:

  • 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.

Hello,
my small test DB for different locations,
i use ADD instead of insert and editrow for update
Input In a list box with immediate storage when leaving the line or retuning. Up or DOWN keys jumps to the next line
Tab key jumps to the next column
see example:
https://www.dropbox.com/s/do1czvnrabxxb5g/sqltest-anlage-testAPI2.xojo_binary_project?dl=1

2 Likes

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. :wink:

Please, DON’T.

Xojo and SQLite are UTF8 native. This is useless and even potentially can break things.

1 Like

oh guys!

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.

:face_with_peeking_eye:

Assuming Xojo and SQLite use UTF8, it makes things a bit easier:

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, _
      CustCompAdd.Text, _
      CustCompPostcode.Text, _
      CustCompCheck1.Text, _
      CustDelName.Text, _
      CustDelAdd.Text, _
      CustDelPostcode.Text, _
      CustName.Text, _
      custTel.Text, _
      custMob.Text, _
      custEmail.Text, _
      custNotes.Text)
      
      Return True
      
    End If
    
  Catch err As DatabaseException
    // Do your DB Errorhandling here...
  End Try
  
  Return False
End Function

Thank you @Rick_A

2 Likes

thank you. The db connect throws an error.
I have a propert setup for the db and an Event Handler On Opening.
Is that correct?

I do appreciate all the replies. I know it must get a bit boring answering newbies all the time.

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. :slight_smile: We like to help and we all started as beginners like you do. We too learned from Forum, Documentations, Books, Classes, Videos, …

All is fine. :slight_smile:

db property

thank you. Yes the property is called db

the event handler looks like this:

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, … ? :slight_smile:
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.

Like:

 If App.db.Connect Then
      
      App.db.ExecuteSQL
1 Like

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.