Skip to content

Syntax for inserting a record on a MySql database

leorio_psu

Asked May 10, 2006, 11:37 PM EDT1 answer
Share
LinkedInX
I got this problem on inserting a record to a certain database.
The solution could be in converting values to be able to save the data on the database.
Please help me on this one.

I use the following codes:

string t1; string sc;
t1=Textbox1.Text;
sc=2;

string ConnStr="Driver={MySQL ODBC 3.51 Driver};" + "Server=localhost;Database=tic";
    OdbcConnection con=new OdbcConnection(ConnStr);
    OdbcCommand cmd=new OdbcCommand("SELECT * FROM player",con);
    con.Open();
    OdbcDataReader MyDataReader;
    MyDataReader=cmd.ExecuteReader();

    cmd.CommandText="INSERT INTO player(name,score) VALUES("+t1+","+sc+")";
    cmd.ExecuteNonQuery();

    MyDataReader.Close();
    con.Close();

whereas the value of t1 is 'lei' based from the value of Textbox1.
Error Message shows:

ERROR [42S21] [MySQL][ODBC 3.51 Driver][mysqld-4.0.16-nt]Unknown column 'lei' in 'field list'

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.Odbc.OdbcException: ERROR [42S21] [MySQL][ODBC 3.51 Driver][mysqld-4.0.16-nt]Unknown column 'lei' in 'field list'

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • +1
    Dont you need single quotes, marked in red in the insert statement

    cmd.CommandText="INSERT INTO player(name,score) VALUES('"+t1+"','"+sc+"')";

    or if score is marked as an integer in the database you dont need the single quotes around the sc.
    +1