
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I create a table and insert values in that table using prepare statements?
It can be understood with the help of following the example in which we have created the table named ‘Student’ by using prepared statement −
mysql> PREPARE stmt3 FROM 'CREATE TABLE Student(Id INT, Name Varchar(20))'; Query OK, 0 rows affected (0.00 sec) Statement prepared mysql> EXECUTE stmt3; Query OK, 0 rows affected (0.73 sec) mysql> DEALLOCATE PREPARE stmt3; Query OK, 0 rows affected (0.00 sec)
Now, with the help of following queries using prepared statements, we can insert the values
in table ‘Student’ −
mysql> PREPARE stmt7 FROM 'INSERT INTO Student(Id,Name) values(?,?)'; Query OK, 0 rows affected (0.00 sec) Statement prepared mysql> SET @A = 1, @B = 'Ram'; Query OK, 0 rows affected (0.00 sec) mysql> EXECUTE stmt7 using @A, @B; Query OK, 1 row affected (0.04 sec) mysql> SET @A = 2, @B = 'Shyam'; Query OK, 0 rows affected (0.00 sec) mysql> EXECUTE stmt7 using @A, @B; Query OK, 1 row affected (0.08 sec) mysql> SET @A = 3, @B = 'Mohan'; Query OK, 0 rows affected (0.00 sec) mysql> Select * from Student; +------+-------+ | Id | Name | +------+-------+ | 1 | Ram | | 2 | Shyam | | 3 | Mohan | +------+-------+ 3 rows in set (0.00 sec)
- Related Questions & Answers
- How can I update a table using prepare statements?
- How can I create a stored procedure to insert values in a MySQL table?
- How can I create a MySQL stored procedure that returns multiple values from a MySQL table?
- INSERT INTO table if a table exists in MySQL else implement CREATE TABLE and create the table
- Insert values from the first table to the second table using two SELECT statements in a single MySQL query
- How can I create a stored procedure to update values in a MySQL table?
- How can I create a stored procedure to delete values from a MySQL table?
- How can I create a table with borders in Android?
- How can I create a MySQL view that takes the values from a table based on some condition(s)?
- Create a table in MySQL that matches another table?
- Can we insert records in a MySQL table without auto_increment values?
- How can I see the CREATE TABLE statement of an existing MySQL table?
- How can I prepare a dish using both vermicelli and seviya?
- How can I create a MySQL table with a column with only 3 possible given values?
- How to create a table with decimal values using JDBC?
Advertisements