In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is python's built-in database". In daily operation, I believe many people have doubts about what python's built-in database is. Xiaobian consulted all kinds of information and sorted out simple and easy to use operation methods. I hope to help you answer the question of "what is python's built-in database"! Next, please follow the small series to learn together!
Input and use
When we say "built-in," it means you don't even have to run pip install to get the library. Simply import by:
import sqlite3 as sl
Create a connection to the database
Don't fret about drivers, connection strings, etc. You can create a SQLite database with a simple connection object:
con = sl.connect('my-test.db')
After running this line of code, we have created the database and connected to it. We ask Python to automatically join existing databases, so it's not empty. Otherwise, we can connect to an existing database using exactly the same code.
create tables
Then create a table:
with con: con.execute(""" CREATE TABLE USER ( id INTEGER NOT NULL PRIMARYKEY AUTOINCREMENT, name TEXT, age INTEGER ); """)
Add three columns to this user table. As you can see, SQLite is indeed lightweight, but it supports all the basic features that a regular RDBMS should have, such as data types, nullability, primary keys, and auto-increment. Running this code creates a table, although it outputs nothing.
insert records
Let's insert some records into the USER table we just created, which also proves that we did create it. Suppose you want to insert multiple entries at once. SQLite in Python makes this easy.
sql = 'INSERT INTO USER (id, name, age) values(?,?, ?) ' data = [ (1, 'Alice', 21), (2, 'Bob', 22), (3, 'Chris', 23) ]
We need to define SQL statements with question marks as placeholders. Then, create some sample data to insert. Insert these sample rows by connecting objects.
with con: con.executemany(sql, data)
After running the code, there is no hint that we succeeded.
lookup table
Now, it's time to test everything. Query the table for sample rows.
with con: data = con.execute("SELECT *FROM USER WHERE age
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.