嗨,我是python和sqlite的新手,我只是测试一个简单的编码,如下所示 import sqlite3
os.chdir("C:\Backup")
conn = sqlite3.connect('FBG.db')
c=conn.cursor()
c.execute('''create table if not exists A (time INTEGER PRIMARY KEY, data REAL)''')
data=(1,2)
c.execute("insert into A (time, data) values (?,?)", data)
c.execute('SELECT * FROM A ORDER BY time').fetchall()
c.close()
所以问题是每一次,我运行代码,我假设它将存储以前的数据,数据库列表将变得更大,但发生的是无论我运行了多少次,所有这个表中的数据仍然是(1,2),但我应该很多(1,2)。所以我想知道是什么原因?因为对我而言,它与“插入”或“插入或替换”没有任何区别。
修改
with open(file, 'rb') as f:
entries = csv.reader(f,delimiter='\t')
rows= list(entries)
###
# For first point (A) three strain values and temperature
###
###use numpy to turn the csv files into a matrix
matrix= numpy.array([])
for i in range (1,5) : #in the setted files using len(list(rows)) to make it read all the coloumn and create a new table for each different coloumn
a=list(rows[3])
result = [0,0]
# the program will detect the number and name for each coloumn then show add new table untill there's no more coloumn left
for r in range(5,len(rows)):
data=list(rows[r])
c.execute("create table if not exists " + str(a[i]) + file[0:4] + file[4:6] + " (time real, data real)") # NEED TO CHANGE TABLE NAME FROM A TO B OR C OR ETC
matrix=numpy.append(matrix,float(data[0]))
matrix=numpy.append(matrix,float(data[i]))
result= numpy.vstack((result,matrix))
matrix=[]
# create a sqlite table named by its point in this case is A with different name to the coloumn
result = numpy.delete(result, (0), axis=0)
for item in result:
c.execute("insert into " + str(a[i]) + file[0:4] + file[4:6] + " values (?,?)", item)
答案 0 :(得分:1)
就像@CL说你不能拥有相同PRIMARY KEY
此表应使用3列构建。
A_ID
PRIMARY KEY AUTO INCREMENT NOT NULL time
DATETIME(可能还有NOT NULL
data
REAL 换句话说,永远不要在代码中设置主键,让数据库自动为您增加主键,或者使用GUID。
这一行
data=(1,2)
是唯一被插入数据库的东西。所以数据库中唯一的记录就是你插入的记录,即1,2