Results 1 to 7 of 7
Thread: Help with sql insert
- 12-01-2008, 09:37 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 12
- Rep Power
- 0
Help with sql insert
I am trying to insert data into my database, but I get the "data type mismatch in criteria expression" error. Everything in the database is stored as text EXCEPT the autonumber field id. It is the first field in the database.
This is my insert:
I tried to insert an empty string into the id field and that's what gives me the error.Java Code:String query = "INSERT INTO Customer VALUES ('','" + fname + "','"+ lname + "','" + address + "','"+ city + "','"+ state + "','"+ zipcode + "','"+ phone + "')";
I just need to know how to deal with the autonumber field...somebody pls help...and thx :)
- 12-01-2008, 09:42 PM #2
Most elegant to insert into sql tables is in this way:
Java Code:INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)
- 12-01-2008, 10:04 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 12
- Rep Power
- 0
Ok so now my insert is as follows:
Java Code:INSERT INTO Customer (fName, lName, address, city, state, zipcode, phone) VALUES ('" + fname + "','"+ lname + "','" + address + "','"+ city + "','"+ state + "','"+ zipcode + "','"+ phone + "')
but I still need to know how to deal with the autonumber field...
- 12-02-2008, 07:48 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Are your table columns actually named "fName" and so? I doubt it.
Also use a PreparedStatement, cobbling together an SQL Statement like this is error-prone and just begging for problems (both of an innocent nature and as SQL injection attacks).
To handle an automatically generated value field, just simply leave that field off the column and value list (i.e. don't include it).
- 12-02-2008, 09:37 AM #5
Member
- Join Date
- Jun 2008
- Posts
- 14
- Rep Power
- 0
Autonumber field is generated by DBMS.
If you need only for that field, try with less field:
INSERT INTO Customer (fName)
VALUES ('" + fname + "')
It will insert a row on your db, only id and fName have value, others null
- 12-02-2008, 09:42 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Like I said, cobbling together an SQL String this way is not the way anyone should want to do this. It is error prone (and outright dangerous).
Only if those columns are defined to allow null values.It will insert a row on your db, only id and fName have value, others null
- 12-02-2008, 07:05 PM #7
Member
- Join Date
- Nov 2008
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
How can i insert a char into a string
By Jamie in forum New To JavaReplies: 8Last Post: 02-17-2011, 08:59 PM -
Cannot insert duplicate key row in object
By losintikfos in forum New To JavaReplies: 3Last Post: 05-07-2009, 09:43 AM -
SQL Insert Help!!!!
By shaungoater in forum New To JavaReplies: 1Last Post: 06-14-2008, 03:14 AM -
How to insert graph in java
By valery in forum Advanced JavaReplies: 1Last Post: 08-06-2007, 08:38 PM -
How do insert a Graphic
By carl in forum New To JavaReplies: 1Last Post: 08-01-2007, 05:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks