6 Replies - 10534 Views - Last Post: 29 September 2012 - 05:23 AM Rate Topic: -----

#1 jappa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-April 09

INSERT INTO using variable

Post icon  Posted 18 April 2009 - 05:45 AM

Hi there,

Im trying to insert variable value into my database. But I cant seem to do it. I am using java language. Below is my intended SQL statement . score is my variable i want to input to database.

  
SQL = "INSERT INTO lettergame (Nickname,Difficulty,Score,TimeElapsed) VALUES ('Keong','Extreme', '" & score & "','03:45:22')";
			
stmt = con.createStatement();
 rs = stmt.executeQuery(SQL);



Help is appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: INSERT INTO using variable

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: INSERT INTO using variable

Posted 18 April 2009 - 05:55 AM

If you're using Java then to append strings you use the + operator, not &.

Were you getting errors before?
Was This Post Helpful? 0
  • +
  • -

#3 jappa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-April 09

Re: INSERT INTO using variable

Posted 18 April 2009 - 06:00 AM

can you show me the right SQL statement for inserting the variable ?

yes it says error "Misssing ;"
Was This Post Helpful? 0
  • +
  • -

#4 jappa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-April 09

Re: INSERT INTO using variable

Posted 18 April 2009 - 06:58 AM

I tried this following code to insert and it did insert the value into the database but there is an exception occur due to this

any idea how to tackle this ?

SQL = "INSERT INTO lettergame (Nickname,Difficulty,Score,TimeElapsed) VALUES(?,?,?,?)";
		   PreparedStatement ps = con.prepareStatement(SQL);
		   ps.setString(1, name);
		   ps.setString(2, diff);
		   ps.setInt(3,score);
		   ps.setString(4,time);
			ps.executeUpdate();




easyjava.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]COUNT field incorrect
Was This Post Helpful? 0
  • +
  • -

#5 jappa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-April 09

Re: INSERT INTO using variable

Posted 18 April 2009 - 07:04 AM

I tried to use this SQL instead and it worked but this time I get different exception error

java.sql.SQLException: No ResultSet was produced

SQL = "INSERT INTO lettergame (Nickname,Difficulty,Score,TimeElapsed) VALUES('"+name+"','"+diff+"','"+score+"','"+time+"')";


Was This Post Helpful? 0
  • +
  • -

#6 Hary  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 427
  • Joined: 23-September 08

Re: INSERT INTO using variable

Posted 20 April 2009 - 01:12 PM

What are the data types of your columns? If your put something between '', it is a string/character type. If it is a numeric type, remove them.
Was This Post Helpful? 0
  • +
  • -

#7 NewbieNeedHelp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 28-September 12

Re: INSERT INTO using variable

Posted 29 September 2012 - 05:23 AM

THANKS @ll

string myInsertQuery = "INSERT INTO test (id) Values ('"+trade.OtherSID+"')";


WORKS!!!

#CLOSED
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1