sql module sqllit

1. Create a database table

Face SQLite database, before we are familiar with SQL commands can be used:

>>> create_table = "create table books (title,author,language)"
>>> cur.execute(create_table)
<sqlite3.Cursor object at 0x104f296c0>

This will set up a table in the database lite.db in books. Data can be added to this table:


>>> cur.execute('insert into books values("python basic","rocky","python")')
<sqlite3.Cursor object at 0x104f296c0>

In order to ensure that data can be saved, but also the following operations:

>>> conn.commit()
>>> cur.close()
>>> conn.close()

Above, the database already has just created a table books, the table already has a record.

2. Query

After saving Let's check what:

>>> conn = sqlite3.connect('lite.db')
>>> cur = conn.cursor()
>>> cur.execute('select * from books')
<sqlite3.Cursor object at 0x104f297a0>
>>> cur.fetchall()[('python basic', 'rocky', 'python')]

3. Bulk Insert

We come to the table books add some more content, in order to carry out other operations to us:

>>> books = [("first book","first","c"),("second book","second","c++"),("third book","third","java")]

This time we come to a bulk insert:

>>> cur.executemany('insert into books values (?,?,?)',books)
<sqlite3.Cursor object at 0x104f297a0>
>>> conn.commit()

Next we print it with a loop to query results:

>>> rows = cur.execute('select * from books')
>>> for row in rows:
...     print(row)
... ('python basic', 'rocky', 'python')('first book', 'first', 'c')('second book', 'second', 'c++')('third book', 'third', 'java')

4. Update

As we said earlier, in cur.execute (), you can write SQL statements to manipulate database:

>>> cur.execute("update books set title='physics' where author='first'")
<sqlite3.Cursor object at 0x104f297a0>
>>> conn.commit()

Then we follow the conditions of inquiry to look at:

>>> cur.execute("select * from books where author='first'")
<sqlite3.Cursor object at 0x104f297a0>
>>> cur.fetchall()[('physics', 'first', 'c')]

5. Delete

Operation of the database must also delete the action:

>>> cur.execute("select * from books")
<sqlite3.Cursor object at 0x104f297a0>
>>> cur.fetchall()
[('python basic', 'rocky', 'python'), ('physics', 'first', 'c'), ('third book', 'third', 'java')]

Finally, do not forget that after the completion of operation of the database, people must remember to "shut the door":

>>> cur.close()
>>> conn.close()
conn.execute('''CREATE TABLE MT
    (ID INT PRIMARY KEY   NOT NULL,
    NAME      TEXT  NOT NULL,
    AGE      INT   NOT NULL,
    ADDRESS    CHAR(50),
    SALARY     REAL);''')
    


除此之外,select语句还可以搭配一些SQLite功能函数使用,常用的有:

    count(*)        统计表内行数(记录数)    sqlite&gt; select count(*) from stu;

    max(column)        计算该列的最大值        columen 是一列的名字

    min(column)        计算该列的最小值

    avg(column)        计算该列的平均值

    sum(column)        计算该列的总和

    upper(column)    输出选中的列的所有字符串的全大写字母

    lower(column)    输出选中的列的所有字符串的全小写字母

    length(column)    计算该列的所有字符串的长度

将这些函数放在select语句内可以完成一些计算工作。例如输入命令

    select sum(score) from student;

可以求出表student内所有学生的成绩总和。

8、模糊查询——like子句

除了使用where子句设定筛选条件外,我们还可以使用like子句模糊匹配表内的记录。like语句搭配两个通配符一起使用:

    百分号%    表示零个、一个或多个数字或字符

    下划线_    表示一个数字或字符

如果某条记录内的某一列能与like后的条件匹配,则该记录被选定。例如:

    select * from student where name like %l%;//从表student中选取名字内带字母l的所有记录
--------------------- 

原文:https://blog.csdn.net/nan_lei/article/details/84344003 
    

Guess you like

Origin www.cnblogs.com/ham-731/p/12121751.html