数据不保存 SQLite3 python3.4 [英] Data not saving SQLite3 python3.4

查看:37
本文介绍了数据不保存 SQLite3 python3.4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试创建一个包含人名和 ip 的 sqlite 数据库虽然我的代码在运行时似乎可以工作,但在运行 SQLite3 ips 后在终端中运行 SELECT * from ips; 时数据没有显示下面是我的代码.它和 SELECT * from ips; 都运行在 ~/Desktop/SQL

I am currently trying to create a sqlite database of peoples names and ip While my code seems to work when I run it the data doesn't show up when I run SELECT * from ips; in terminal after running SQLite3 ips Below is my code. Both it and the SELECT * from ips; are running in ~/Desktop/SQL

import sqlite3 as sql
import socket
import struct
def iptoint(ip):
    return str(struct.unpack("i",socket.inet_aton(ip))[0])


database = sql.connect("ips")
createTable = True
if createTable:
    database.execute('''CREATE TABLE main.ips
    (FIRST_NAME TEXT PRIMARY KEY NOT NULL,
    SECOND_NAME TEXT NOT NULL,
    IP INT32 NOT NULL);''')

sampleIps = [("Edward","E","60.222.168.44")]
for first,second,ip in sampleIps:
    string = "INSERT INTO ips VALUES ('%s','%s','%s');"%(first,second,iptoint(ip))
    print(string)
    #Printing the string gives me INSERT INTO ips VALUES ('Edward','E','749264444');
    database.execute("INSERT INTO ips VALUES ('%s','%s','%s');"%(first,second,iptoint(ip)))

database.close()

我的电脑运行的是 OSX 10.11.4、python 3.4 和 SQLite 3.14.1

My computer is running OSX 10.11.4, python 3.4 and SQLite 3.14.1

我尝试将 ips 更改为 main.ips 并返回

I have tried changing ips to main.ips and back

推荐答案

您似乎没有提交到数据库.您需要在关闭连接之前提交才能实际保存对数据库的更改.

It doesn't look like you are committing to the database. You need to commit before closing the connection in order to actually save your changes to the database.

database.commit()

这篇关于数据不保存 SQLite3 python3.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆