我有一個腳本要求輸入,然後將該輸入插入到表中。 腳本運行的下一次,我想讓它告訴用戶最近一次輸入的id是什麼。無法從SQLITE3數據庫獲取最後一行ID
表是這樣的:ID INTEGER PRIMARY KEY AUTOINCREMENT,用戶標識文本,文本域,密碼文本,網絡服務器TEXT,SQLSERVER TEXT
我被告知我可以使用SELECT SEQ從SQLITE_SEQUENCE其中name = '表名',但它會產生以下文本:而不是最後一行的id。
請注意,我是一個非常新的Python/SQLite3編碼器!
供您參考,代碼SOFAR看起來是這樣的:
#!/usr/bin/python
import os, sys, sqlite3
######## CHECK SYSTEM COMPATIBILITY ########
if os.name =='posix':
os.system("clear")#CLEAR SCREEN#
pass
else:
sys.exit("Operating System is not supported")
######## END CHECK SYSTEM COMPATIBILITY ########
######## CHECK IF SCRIPT IS RUN AS ROOT ########
#if os.geteuid() != 0:
# sys.exit("Script must be run as root")
#else:
# pass
####### END CHECK IF SCRIPT IS RUN AS ROOT ########
####### CREATE DATABASE AND CHECK IF TABLE EXISTS ##########
conn = sqlite3.connect("dat.db")
c = conn.cursor()
c.execute ('''CREATE TABLE IF NOT EXISTS kunder
(id INTEGER PRIMARY KEY AUTOINCREMENT, userid TEXT, domain TEXT, password TEXT, webserver TEXT, sqlserver TEXT)''')
conn.commit()
print c.execute ("SELECT seq from SQLITE_SEQUENCE WHERE name='kunder'")
conn.close()
######## DONE CREATE DATABASE AND CHECK IF TABLE EXISTS #########
###### ASK FOR INPUT ##########
########### HERE NEEDS TO BE A CHECK TO DETERMINE THE LATEST USERID - ALSO NEEDS TO BE FOR WEBSERVER AND PASSWORD #################
userid = raw_input("Enter userid: ")
########### HERE NEEDS TO BE A CHECK TO SEE IF USERID EXISTS!!!!!#####################
domain = raw_input("Enter domain: ")
password = raw_input("Enter password: ")
########### NEEDS TO BE A WAY TO AUTOGENERATE A PASSWORD!!! ####################
webserver = raw_input("Enter webserver: ")
sqlserver = raw_input("Enter sqlserver: ")
###### FINISHED ASK FOR INPUT #######
######## DATABASE ###########
conn = sqlite3.connect("dat.db")
c = conn.cursor()
c.execute ("INSERT INTO kunder (userid, domain, password, webserver, sqlserver) VALUES (?,?,?,?,?)", (userid, domain, password, webserver, sqlserver))
conn.commit()
conn.close()
####### DONE WITH DATABASE ##########
print c.execute(「SELECT max(id)FROM kunder」)會產生而不是id –
MadsRC
我剛剛給你使用了sql語句。您需要從光標讀取數據。我會更新我的答案以展示如何做到這一點,但您應該閱讀文檔以更好地理解它。 – aquavitae
謝謝:) 編程和數據庫不是我的強項,我今天和昨天都在搜索解決方案。有了解決方案,我可以研究它爲什麼做它的功能。 – MadsRC