我目前正在尝试创建一个包含人名和ip的sqlite数据库,虽然我的代码在运行时似乎可以正常运行,但是SELECT * from ips;在运行SQLite3 ips 下面的代码后在终端中运行时,数据却无法显示。它和SELECT * from ips;都在〜/ Desktop / SQL中运行
SELECT * from ips;
SQLite3 ips
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
我尝试将ips更改为main.ips并返回
您似乎没有提交到数据库。您需要在关闭连接之前进行提交,才能将更改实际保存到数据库中。
database.commit()