Inserting AutoIncrement rows

Dman

Active Member
Licensed User
Longtime User
I'm having an issue and can't figure out why this gives me a datatype mismatch.

I have attached a test project and hope someone can steer me where I have went wrong.

Here is the code also.

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
Dim SQL1 As SQL

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim lblRow As Label
   Dim txtName As EditText
   Dim btnInsert As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Autoinctest")
   '****Check and load the database****
    If File.Exists(File.DirInternal, "autoinctest.db") = False Then
      File.Copy(File.DirAssets, "autoinctest.db", File.DirInternal, "autoinctest.db")
   End If
         
   '****Initialize the database****         
   SQL1.Initialize(File.DirInternal, "autoinctest.db", False)
   '*******************************
End Sub


Sub btnInsert_Click

      SQL1.ExecNonQuery2("INSERT INTO table1 VALUES (?,?)", Array As String(Null, txtName.Text))

End Sub
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
I am presuming that the Null value you are trying to insert is for the autoincrement field. If this is the case, you can't insert a value into that field as the database takes care of that value. You'd just need to insert values into fields other than that one like:

B4X:
SQL1.ExecNonQuery2("INSERT INTO table1 (columnlabel) VALUES (?)", Array As String( txtName.Text))

Replace columnlabel above with the name of the column you are inserting into.
 
Upvote 0

pluton

Active Member
Licensed User
Longtime User
If you set in your database that field as autoincrement just skip that field because automaticly will be increased.
So as Jack Cole says
B4X:
SQL1.ExecNonQuery2("INSERT INTO table1 VALUES (?)", Array As String(txtName.Text))
 
Upvote 0

Dman

Active Member
Licensed User
Longtime User
Thanks guys. That makes a little sense now. I'm old but trying to learn.

I try to use the search function as much as possible but sometimes those of us that don't know what we are looking for have problems. We know what we want to do, we just might not know the technical name for it.
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
No prob Dman. Android is different (than windoze). I have tried, read and fooled with autoinc and still can't make it work.... (it was simple before SQL)

I also have much trouble finding a solution to an issue by searching the posts - even thou it has been addressed many times before. I usually experiment and study my problem before I resort to asking for help. When I am really stuck, I reach out, knowing what specific question to ask.

I see that as the community grows, links are provided to a previous explanation rather than re-typing the suggested solution. I still don't know how they find them so fast...

I suppose what we (old farts) really need is a tutorial in how to search effectively - if there is such a thing (perhaps there is but I have not found it). I can't even master including a simple link or formating code in a post... :sign0161: I have accomplished the emoticon (if that is what it is called).

I often, when time permits, scan every question post - and answer for jewels. Amazing what one learns from these. As days past, retension and finding them again becomes the problem - right back at the search issue again... I can't offer a (kmnowledge base) solution.

A VERY BIG THANKS TO ALL OF YOU THAT HELP ALL OF US ALONG OUR WAY!
Now there are many fine folks.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…