我目前正在尝试创建人名和ip的sqlite数据库
虽然我的代码在运行时似乎有效,但在运行SELECT * from ips;
后在终端中运行SQLite3 ips
时,数据不会显示
以下是我的代码。它和SELECT * from ips;
都在〜/ 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
我尝试将ips更改为main.ips并返回
答案 0 :(得分:1)
看起来你没有提交到数据库。您需要在关闭连接之前提交,以便将更改实际保存到数据库中。
database.commit()