0

I am new on this. I did the following programming sequence in order to delete all rows from a table i have created recently.

import sqlite3
con=sqlite3.connect('prueba.db')
cur=con.cursor()
cur.execute('''CREATE TABLE tabla1(ExtremoA text,ExtremoZ text,PotenciaRxA real,PotenciaTxA real,PotenciaRxZ real,PotenciaTxZ real,SpanLossAZ real,SpanLossZA real)''')
print "Tabla1 creada exitosamente"
cur.close()
con.close()
con=sqlite3.connect('prueba.db')
cur=con.cursor()
registros=[('Quillota','San felipe',-9,4,-11,3,15,12),('San Felipe','Las Cuevas',-10,2,-12,6,16,14)]
sql='''INSERT INTO tabla1(ExtremoA,ExtremoZ,PotenciaRxA,PotenciaTxA,PotenciaRxZ,PotenciaTxZ,SpanLossAZ,SpanLossZA)VALUES(?,?,?,?,?,?,?,?)'''
cur.executemany(sql,registros)
con.commit()
print "registros creados exitosamente"
cur.execute('''SELECT * FROM tabla1''')
todo=cur.fetchall()
print todo
print len(todo)
for i in range(len(todo)):
    cur.execute('''DELETE FROM tabla1 WHERE rowid=?''',(i,))
con.commit()
print "número total de filas borradas:", con.total_changes
cur.close()
con.close()

My Table "tabla1" have two entries. When python finishes and I perform SELECT and fetchall once more, I realize that only the first entry has been deleted. What is the problem?. should I declarate or create a rowid column field at the beginning ?. Sorry for my grammar.

1 Answer 1

3

range(2) returns 0 and 1, but your rowid values are 1 and 2.

Anyway, to delete all rows from a table, just omit the WHERE clause from the DELETE statement.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.