I am making a small script that, among other things, makes a database with four entries called id, x, y and z. Ids should be unique and sequential, so I'm using the "primary key" id for every entry. The other values can be any real numbers, but the database should not contain two entries for which all three x, y and z are the same.
So the table is defined like this:
if __name__ == "__main__":
c.execute('drop table nodes')
c.execute('create table nodes (id integer primary key, x real, y real, z real)')
Then I insert new entries into the table as:
c.execute('select * from nodes where x={x} and y={y} and z={z}'.format(x=x,y=y,z=z))
if len(c.fetchall())==0:
q="""
INSERT INTO nodes
(`x`, `y`, `z`)
VALUES
(%f,%f,%f)
""" % (x,y,z)
c.execute(q)
This sort of works, but it gets very slow as the database grows bigger. Is there a faster way to insert entries into the database while making sure they are an unique combination of values?