In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use Python built-in database". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Python built-in database".
Import and u
What we mean by "built-in" here is that you don't even have to run pip install to get the library. You only need to import it in the following ways:
Import sqlite3 as sl
1. Create a connection to the database
We don't need to worry about drivers, connection strings, and so on. You can create a SQLite database directly and have a simple connection object:
Con = sl.connect ('my-test.db')
After running this line of code, we have created and connected to the database. If the database that requires the Python connection does not exist, it will automatically create an empty database for us. If we have already created the database, we can connect to the existing database with exactly the same code as above.
two。 Create a table
Next, let's create a table.
With con: con.execute ("CREATE TABLE USER (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER);")
In this USER table, we added three columns. As we can see, SQLite is indeed lightweight, but it supports all the basic features that a regular RDBMS should have, such as data types, null, primary keys, and auto-increment.
After running this code, we have created a table, even though it doesn't output anything.
3. Insert record
Let's insert a data record in the USER table we just created, which proves that we did create it.
If we need to insert multiple records at once, SQLite in Python can also easily do this.
Sql = 'INSERT INTO USER (id, name, age) values (?) Data = [(1, 'Alice', 21), (2,' Bob', 22), (3, 'Chris', 23)]
We need to define the SQL statement using the question mark as a placeholder. The next step is to create some sample data to insert. Using the connection object, you can insert these sample routines.
With con: con.executemany (sql, data)
If there is no error after running the code, it is successful.
4. Query form
Next, we verify what we have done in a practical way, and get the sample routine by querying the table.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.