The question mark in the SQL statement learning notes

Source: Internet
Author: User
Tags mysql manual

I see an instance of

The code is as follows Copy Code
String sql = "Select Userid,name from Tuser WHERE userid=?" and password=? ";
pstmt = conn.preparestatement (sql);
Pstmt.setstring (1,userid); Set the first one here? The value
Pstmt.setstring (2,password); The second one is set here? The value

After you "setstring" all of this? , your SQL is built up.

Well, if I still don't understand, I've found another article.

Reference I:

The code is as follows Copy Code

SELECT * FROM table where username= ' syy913 '

Username= ' syy913 '

followed by a string. In the database is used when the quotation marks. There is a mantra called both Gaga alone.

Used only in the database, the dual is used in the external programming language, in C # Java and other languages are used in the double quotes identified by the string.

The code is as follows Copy Code

String Sql= "select * from table", which identifies a string.

String Sql= "select * FROM table" + "where id>0″;2 string connection


Below the

The code is as follows Copy Code

String Sql= "select * from table where id>0″+" and name=?;

This question mark indicates the parameter to pass. The parameter must be given to him before executing this SQL, and the name above is character-type. You're looking at what it means to write.

The code is as follows Copy Code

String nn= "Kill"

String Sql= "select * from table where id>0 and name=?";

Because? represents a string.

The nn output to see he is not "" this.

Then it should be ' nn ' to the database, i.e.

The code is as follows Copy Code

String Sql= "select * from table where id>0 and name= ' nn '";

On it, why do you want the inside + double quotes?

The code is as follows Copy Code

String Sql= "select * from table where id>0 and Name= '" +nn+ "";

I gave him nn as a variable.

Reference II:

This SQL statement can be used with a question mark ("?") if it contains parameters. To take a placeholder for a parameter without immediately assigning a value to the parameter, and before the statement executes, you must assign a value to the argument at the question mark by the appropriate set*** (). In New Parseinfo (), the SQL statement that contains the parameters is decomposed into multiple segments and placed in Staticsql so that the position of the parameter is required when the parameter is set. Suppose the SQL statement is "SELECT * from adv where id =?" and name =? " , then the elements in Staticsql are 3, staticsql[3]={"select * from adv WHERE id =", "and name =", ""}. Note that the last element in the array, in this case, is "", because the last one in my example is "?", if the SQL statement is such "select * from adv where id =?" and name =? Order by ID, Staticsql becomes this way {"SELECT * from adv WHERE id =", "and name =", "ORDER by id"}.

Irrelevant reference three: (MySQL manual C API preprocessing statements)

25.2.7.4. Mysql_stmt_bind_param ()

My_bool Mysql_stmt_bind_param (mysql_stmt *stmt, Mysql_bind *bind)

Describe

Mysql_stmt_bind_param () is used to bind data to a parameter marker in an SQL statement to pass to Mysql_stmt_prepare (). It uses the MYSQL_BIND structure to provide data. "Bind" is the address of an array of mysql_bind structures. As expected by the client library, for each "?" that appears in the query. Parameter markers that contain 1 elements in the array.

Suppose you prepared the following statement:

The code is as follows Copy Code

INSERT into Mytbl VALUES (?,?,?) When binding parameters, an array of mysql_bind structures contains 3 elements and can be declared as follows:

Mysql_bind Bind[3];

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.