python3.8.1 SQLite3 up a data table

1. Link to database SQLite3 database

#!/usr/bin/python

import sqlite3

conn = sqlite3.connect('test.db')
print("打开数据库test.db成功!")
c = conn.cursor()
c.execute('''DROP TABLE COMPANY;''')
c.execute('''CREATE TABLE COMPANY \
       (ID INT PRIMARY KEY     NOT NULL, \
       NAME           TEXT    NOT NULL, \
       AGE            INT     NOT NULL, \
       ADDRESS        CHAR(50), \
       SALARY         REAL);''')
print("建立表company成功!")
conn.commit()
conn.close()

2. Run results

python.exe sqlite3Python.py
open the database test.db success!
Table established company success!

3.SQlite3 common database command

No. API & Description
1 sqlite3.connect(database [,timeout ,other optional arguments])

The API opens a SQLite database file database link. You can use ": memory:" to open a database connection to the database in RAM, instead of opening on the disk. If the database is successfully opened, a connection object is returned.

When a database is connected to a plurality of access, and in which a database is modified, this time SQLite database is locked until the transaction commits. timeout parameter represents the connection duration waiting for a lock, until the occurrence of an abnormal disconnect. The default timeout parameter is 5.0 (five seconds).

If the given name of the database filename does not exist, the call will create a database. If you do not want to create the database in the current directory, you can specify the file name with a path so that you can create a database anywhere.

2 connection.cursor([cursorClass])

This routine creates a  the Cursor , it will be used in database programming in Python. The method accepts a single optional parameter cursorClass. If this parameter is provided, it must be extended from a custom category sqlite3.Cursor the cursor.

3 cursor.execute(sql [, optional parameters])

The routine execute a SQL statement. The SQL statement can be parameterized (i.e., instead of using the SQL text placeholder). sqlite3 module supports two types of placeholders: question marks and named placeholders (named style).

例如:cursor.execute("insert into people values (?, ?)", (who, age))

4 connection.execute(sql [, optional parameters])

The routine is a shortcut method provided by the cursor (Cursor) on the object above, it creates an intermediate cursor object by calling the cursor (Cursor) method, the Execute method and invoke the cursor by the given parameters.

5 cursor.executemany(sql, seq_of_parameters)

The routine mapping of all parameters or to perform seq_of_parameters in a SQL command.

6 connection.executemany(sql[, parameters])

This routine is called by a cursor object shortcuts cursor intermediate (Cursor) method creates, executemany method then calls the cursor by the given parameters.

7 cursor.executescript(sql_script)

The routine Upon receiving the script, execute multiple SQL statements. It first COMMIT statement is executed, and then execute the SQL script as a parameter. All SQL statements should use a semicolon (;) separated.

8 connection.executescript(sql_script)

This routine is called by a cursor object shortcuts cursor intermediate (Cursor) method creates, executescript method then calls the cursor by the given parameters.

9 connection.total_changes()

The routine returns to be modified since the database connection is open, insert, or delete the number of head office database.

10 connection.commit()

This method commits the current transaction. Since this method if you do not call, then from your first call commit () any other action made to the database connection is not visible.

11 connection.rollback()

The changes made to the database since the last call to the rollback method commit () since.

12 connection.close()

The method closes the database connection. Please note that this does not automatically invoke commit (). If you do not commit before calling () method, you close the database connection, all your changes will be lost!

13 cursor.fetchone ()

该方法获取查询结果集中的下一行,返回一个单一的序列,当没有更多可用的数据时,则返回 None。

14 cursor.fetchmany([size=cursor.arraysize])

该方法获取查询结果集中的下一行组,返回一个列表。当没有更多的可用的行时,则返回一个空的列表。该方法尝试获取由 size 参数指定的尽可能多的行。

15 cursor.fetchall()

该例程获取查询结果集中所有(剩余)的行,返回一个列表。当没有可用的行时,则返回一个空的列表。

 

 

发布了181 篇原创文章 · 获赞 13 · 访问量 2万+

Guess you like

Origin blog.csdn.net/keny88888/article/details/104597665