• SQLite


    import sqlite3
    #连接数据库
    conn = sqlite3.connect("test.db")   #打开或创建数据库文件(当不存在时会创建)
    print("Opened database successfully")
    
    c = conn.cursor()   #获取游标
    sql0 = '''drop table company;'''
    
    sql_create = '''
        create table company
            (id int primary key not null,
            name text not null,
            age int not null,
            address char(50),
            salary real);
    '''
    sql_insert_1 = '''
        insert into company (id,name,age,address,salary)
        values(1,"monkey",32,"杭州",10000)'''
    sql_insert_2 = '''
        insert into company (id,name,age,address,salary)
        values(2,"pig",32,"杭州",20000)'''
    
    sql_select= '''SELECT * FROM company;'''
    # c.execute(sql_create)   #执行SQL语句
    # conn.commit()   #提交
    # c.execute(sql_insert_1)
    # c.execute(sql_insert_2)
    # conn.commit()
    c.execute(sql_select)
    cursor = c.execute(sql_select)
    for row in  cursor:
        print("id = ",row[0])
        print("name = %s"%row[1])
        print("address = ",row[2])
        print("salary = ",row[3],"
    ")
    conn.close      #关闭数据库
    
    print("执行SQL成功")
  • 相关阅读:
    作业一
    工作日志(二)
    工作日志(三)
    工作日志(四)
    工作日志(一)
    团队项目开发日志(四)
    团队项目开发日志(三)
    团队项目开发日志(二)
    团队项目开发日志(一)
    第四次作业
  • 原文地址:https://www.cnblogs.com/uphold/p/13923415.html
Copyright © 2020-2023  润新知