python - sql + pandas 与 sqlite 结合
发布日期:2021-06-30 19:51:16 浏览次数:2 分类:技术文章

本文共 1445 字,大约阅读时间需要 4 分钟。

SQLite

  • 关联式资料库-SQLite
  • Firefox的附加组件中搜索安装即可
  • python 自带sqlite3 模块
# coding: utf-8# In[2]:import sqlite3 as dbcon = db.connect('test.sqlite')cur = con.cursor()sql = 'SELECT SQLITE_VERSION()'cur.execute(sql)data = cur.fetchone()print(data)con.close()# In[3]:with db.connect('test.sqlite') as con:    cur = con.cursor()    sql = 'SELECT SQLITE_VERSION()'    cur.execute(sql)    data = cur.fetchone()    print(data)    # In[3]:with db.connect("test.sqlite") as con:     cur = con.cursor()     cur.execute("DROP TABLE IF EXISTS PhoneAddress")     cur.execute("CREATE TABLE PhoneAddress(phone CHAR(10) PRIMARY KEY, address TEXT, name TEXT unique, age INT NOT NULL)")     cur.execute("INSERT INTO PhoneAddress VALUES('0912173381','United State','Jhon Doe',53)")     cur.execute("INSERT INTO PhoneAddress(phone, address, name, age) VALUES('0928375018','Tokyo Japan','MuMu Cat',6)")     cur.execute("INSERT INTO PhoneAddress VALUES('0957209108','Taipei','Richard',29)")     cur.execute('SELECT * FROM PhoneAddress')    data = cur.fetchone()    print(data[0], data[1])    print('============================')    rows = cur.fetchall()    for item in rows:        print(item[0], item[1])# In[4]:import pandasemployee = [{'name':'Mary', 'age':23 , 'gender': 'F'},{'name':'John', 'age':33 , 'gender': 'M'}]df = pandas.DataFrame(employee)df# In[5]:with db.connect('test.sqlite') as con:    df.to_sql(name = 'employee', con = con, if_exists='replace', index = None)

转载地址:https://lipenglin.blog.csdn.net/article/details/78012331 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:python - 使用sql 分析(06 - 15)国内各省GDP
下一篇:python - 【用户、商品】【购买、浏览】数据处理

发表评论

最新留言

不错!
[***.144.177.141]2024年04月11日 03时55分56秒