untung99play.xyz: Insert Data Into a Table Examples
Untung99 menawarkan beragam permainan yang menarik, termasuk slot online, poker, roulette, blackjack, dan taruhan olahraga langsung. Dengan koleksi permainan yang lengkap dan terus diperbarui, pemain memiliki banyak pilihan untuk menjaga kegembiraan mereka. Selain itu, Untung99 juga menyediakan bonus dan promosi menarik yang meningkatkan peluang kemenangan dan memberikan nilai tambah kepada pemain.
Berikut adalah artikel atau berita tentang Harian untung99play.xyz dengan judul untung99play.xyz: Insert Data Into a Table Examples yang telah tayang di untung99play.xyz terimakasih telah menyimak. Bila ada masukan atau komplain mengenai artikel berikut silahkan hubungi email kami di koresponden@untung99play.xyz, Terimakasih.
Summary: in this tutorial, you will learn how to insert data into a table using MySQL Connector/Python API.
To insert new rows into a MySQL table, you follow these steps:
- Connect to the MySQL database server by creating a new
MySQLConnection
object. - Initiate a
MySQLCursor
object from theMySQLConnection
object. - Execute the
INSERT
statement to insert data into the table. - Close the database connection.
MySQL Connector/Python provides API that allows you to insert one or multiple rows into a table at a time. Let’s examine at each method in more detail.
Insert one row into a table
The following method inserts a new book into the books
table:
from mysql.connector import MySQLConnection, Error
from python_mysql_dbconfig import read_db_config
def insert_book(title, isbn):
query = "INSERT INTO books(title,isbn) " \
"VALUES(%s,%s)"
args = (title, isbn)
try:
db_config = read_db_config()
conn = MySQLConnection(**db_config)
cursor = conn.cursor()
cursor.execute(query, args)
if cursor.lastrowid:
print('last insert id', cursor.lastrowid)
else:
print('last insert id not found')
conn.commit()
except Error as error:
print(error)
finally:
cursor.close()
conn.close()
def main():
insert_book('A Sudden Light','9781439187036')
if __name__ == '__main__':
main()
Code language: Python (python)
In the above code:
- First, import
MySQLConnection
andError
objects from the MySQL Connector/Python package andread_db_config()
function from the python_mysql_dbconfig module. - Next, define a new function named
insert_book()
that accepts two arguments: title and isbn. Inside theinsert_book()
function, construct anINSERT
statement (query
) and data (args
) for inserting into thebooks
table. Notice that the data passing to the function is a tuple. - Then, create a new connection, execute the statement, and commit the change in the
try except
block. Note that you have to explicitly call thecommit()
method in order to make the changes to the database. In case a new row is inserted successfully, you can retrieve the last insert id of the AUTO_INCREMENT column by using thelastrowid
property of theMySQLCursor
object. - After that, close the cursor and database connection at the end of the
insert_book()
function. - Finally, call the
insert_book()
function to insert a new row into thebooks
table in themain()
function.
Insert multiple rows into a table
The following INSERT
statement allows you to insert multiple rows into the books
table:
INSERT INTO books(title,isbn)
VALUES('Harry Potter And The Order Of The Phoenix', '9780439358071'),
('Gone with the Wind', '9780446675536'),
('Pride and Prejudice (Modern Library Classics)', '9780679783268');
Code language: SQL (Structured Query Language) (sql)
To insert multiple rows into a table in Python, you use the executemany()
method of the MySQLCursor
object. See the following code:
from mysql.connector import MySQLConnection, Error
from python_mysql_dbconfig import read_db_config
def insert_books(books):
query = "INSERT INTO books(title,isbn) " \
"VALUES(%s,%s)"
try:
db_config = read_db_config()
conn = MySQLConnection(**db_config)
cursor = conn.cursor()
cursor.executemany(query, books)
conn.commit()
except Error as e:
print('Error:', e)
finally:
cursor.close()
conn.close()
def main():
books = [('Harry Potter And The Order Of The Phoenix', '9780439358071'),
('Gone with the Wind', '9780446675536'),
('Pride and Prejudice (Modern Library Classics)', '9780679783268')]
insert_books(books)
if __name__ == '__main__':
main()
Code language: Python (python)
The logic in this example is similar to the logic in the first example. However, instead of calling the execute()
method, we call executemany()
method.
In the main()
function, we pass a list of tuples, each contains title and isbn of the book to the insert_books()
function.
By calling the executemany()
method of the MySQLCursor
object, the MySQL Connector/Python translates the INSERT
statement into the one that contains multiple lists of values.
In this tutorial, you have learned how to insert one or more rows into a table in Python.
Was this tutorial helpful?