Python and SQL - most efficient way to check if Row exists - if not INSERT it
up vote
0
down vote
favorite
I would like to check what is the most efficient Python method (because of the performances) how to check if some row exists in SQL, and if not, then insert it.
I want to check if for some value, row exists in table and if not insert it.
I did it like this , but I was wondering if there is some better way.
insertValue="test"
mycursor.execute("SELECT * from CUSTOMERS where name ="+'insertValue')
myresult2 = mycursor.fetchall()
if not myresult2:
print 'Row does not EXIST'
mycusrsor.execute(.....INSERT part for insertValue in table TABLE_2.....)
Is this this optimal method for Python execution (code optimization), or this can be done in more efficient way (from the perspective of Python code optimization)
Thanks
python mysql python-2.7 insert
add a comment |
up vote
0
down vote
favorite
I would like to check what is the most efficient Python method (because of the performances) how to check if some row exists in SQL, and if not, then insert it.
I want to check if for some value, row exists in table and if not insert it.
I did it like this , but I was wondering if there is some better way.
insertValue="test"
mycursor.execute("SELECT * from CUSTOMERS where name ="+'insertValue')
myresult2 = mycursor.fetchall()
if not myresult2:
print 'Row does not EXIST'
mycusrsor.execute(.....INSERT part for insertValue in table TABLE_2.....)
Is this this optimal method for Python execution (code optimization), or this can be done in more efficient way (from the perspective of Python code optimization)
Thanks
python mysql python-2.7 insert
First optimization would be to not useSELECT *
instead, doSELECT <primary key>
– Madhur Bhaiya
Nov 11 at 10:40
Secondly, add index on thename
column.
– Madhur Bhaiya
Nov 11 at 10:40
I was thinking more from the Python perspective, code optimization for execution - not SQL. Sorry for misunderstanding. I clarified in more details this now
– Dejan
Nov 11 at 10:43
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I would like to check what is the most efficient Python method (because of the performances) how to check if some row exists in SQL, and if not, then insert it.
I want to check if for some value, row exists in table and if not insert it.
I did it like this , but I was wondering if there is some better way.
insertValue="test"
mycursor.execute("SELECT * from CUSTOMERS where name ="+'insertValue')
myresult2 = mycursor.fetchall()
if not myresult2:
print 'Row does not EXIST'
mycusrsor.execute(.....INSERT part for insertValue in table TABLE_2.....)
Is this this optimal method for Python execution (code optimization), or this can be done in more efficient way (from the perspective of Python code optimization)
Thanks
python mysql python-2.7 insert
I would like to check what is the most efficient Python method (because of the performances) how to check if some row exists in SQL, and if not, then insert it.
I want to check if for some value, row exists in table and if not insert it.
I did it like this , but I was wondering if there is some better way.
insertValue="test"
mycursor.execute("SELECT * from CUSTOMERS where name ="+'insertValue')
myresult2 = mycursor.fetchall()
if not myresult2:
print 'Row does not EXIST'
mycusrsor.execute(.....INSERT part for insertValue in table TABLE_2.....)
Is this this optimal method for Python execution (code optimization), or this can be done in more efficient way (from the perspective of Python code optimization)
Thanks
python mysql python-2.7 insert
python mysql python-2.7 insert
edited Nov 11 at 10:54
asked Nov 11 at 10:37
Dejan
54982147
54982147
First optimization would be to not useSELECT *
instead, doSELECT <primary key>
– Madhur Bhaiya
Nov 11 at 10:40
Secondly, add index on thename
column.
– Madhur Bhaiya
Nov 11 at 10:40
I was thinking more from the Python perspective, code optimization for execution - not SQL. Sorry for misunderstanding. I clarified in more details this now
– Dejan
Nov 11 at 10:43
add a comment |
First optimization would be to not useSELECT *
instead, doSELECT <primary key>
– Madhur Bhaiya
Nov 11 at 10:40
Secondly, add index on thename
column.
– Madhur Bhaiya
Nov 11 at 10:40
I was thinking more from the Python perspective, code optimization for execution - not SQL. Sorry for misunderstanding. I clarified in more details this now
– Dejan
Nov 11 at 10:43
First optimization would be to not use
SELECT *
instead, do SELECT <primary key>
– Madhur Bhaiya
Nov 11 at 10:40
First optimization would be to not use
SELECT *
instead, do SELECT <primary key>
– Madhur Bhaiya
Nov 11 at 10:40
Secondly, add index on the
name
column.– Madhur Bhaiya
Nov 11 at 10:40
Secondly, add index on the
name
column.– Madhur Bhaiya
Nov 11 at 10:40
I was thinking more from the Python perspective, code optimization for execution - not SQL. Sorry for misunderstanding. I clarified in more details this now
– Dejan
Nov 11 at 10:43
I was thinking more from the Python perspective, code optimization for execution - not SQL. Sorry for misunderstanding. I clarified in more details this now
– Dejan
Nov 11 at 10:43
add a comment |
1 Answer
1
active
oldest
votes
up vote
-1
down vote
From the tags used it seems you are using "MYSQL". It provides an option as "INSERT IGNORE" which means that if the key is already present, it will not throw an execution error and will move on to the next row.
This will prove to be the most efficient way and with very less code.
It will work only when eithername
is PK orname
is set to UNIQUE
– Madhur Bhaiya
Nov 11 at 11:13
Name is not PK and also it is not set to UNIQUE
– Dejan
Nov 11 at 11:49
Do mention such constraints before hand itself
– swapnil shashank
Nov 11 at 11:53
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
-1
down vote
From the tags used it seems you are using "MYSQL". It provides an option as "INSERT IGNORE" which means that if the key is already present, it will not throw an execution error and will move on to the next row.
This will prove to be the most efficient way and with very less code.
It will work only when eithername
is PK orname
is set to UNIQUE
– Madhur Bhaiya
Nov 11 at 11:13
Name is not PK and also it is not set to UNIQUE
– Dejan
Nov 11 at 11:49
Do mention such constraints before hand itself
– swapnil shashank
Nov 11 at 11:53
add a comment |
up vote
-1
down vote
From the tags used it seems you are using "MYSQL". It provides an option as "INSERT IGNORE" which means that if the key is already present, it will not throw an execution error and will move on to the next row.
This will prove to be the most efficient way and with very less code.
It will work only when eithername
is PK orname
is set to UNIQUE
– Madhur Bhaiya
Nov 11 at 11:13
Name is not PK and also it is not set to UNIQUE
– Dejan
Nov 11 at 11:49
Do mention such constraints before hand itself
– swapnil shashank
Nov 11 at 11:53
add a comment |
up vote
-1
down vote
up vote
-1
down vote
From the tags used it seems you are using "MYSQL". It provides an option as "INSERT IGNORE" which means that if the key is already present, it will not throw an execution error and will move on to the next row.
This will prove to be the most efficient way and with very less code.
From the tags used it seems you are using "MYSQL". It provides an option as "INSERT IGNORE" which means that if the key is already present, it will not throw an execution error and will move on to the next row.
This will prove to be the most efficient way and with very less code.
answered Nov 11 at 11:03
swapnil shashank
625
625
It will work only when eithername
is PK orname
is set to UNIQUE
– Madhur Bhaiya
Nov 11 at 11:13
Name is not PK and also it is not set to UNIQUE
– Dejan
Nov 11 at 11:49
Do mention such constraints before hand itself
– swapnil shashank
Nov 11 at 11:53
add a comment |
It will work only when eithername
is PK orname
is set to UNIQUE
– Madhur Bhaiya
Nov 11 at 11:13
Name is not PK and also it is not set to UNIQUE
– Dejan
Nov 11 at 11:49
Do mention such constraints before hand itself
– swapnil shashank
Nov 11 at 11:53
It will work only when either
name
is PK or name
is set to UNIQUE– Madhur Bhaiya
Nov 11 at 11:13
It will work only when either
name
is PK or name
is set to UNIQUE– Madhur Bhaiya
Nov 11 at 11:13
Name is not PK and also it is not set to UNIQUE
– Dejan
Nov 11 at 11:49
Name is not PK and also it is not set to UNIQUE
– Dejan
Nov 11 at 11:49
Do mention such constraints before hand itself
– swapnil shashank
Nov 11 at 11:53
Do mention such constraints before hand itself
– swapnil shashank
Nov 11 at 11:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247909%2fpython-and-sql-most-efficient-way-to-check-if-row-exists-if-not-insert-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
First optimization would be to not use
SELECT *
instead, doSELECT <primary key>
– Madhur Bhaiya
Nov 11 at 10:40
Secondly, add index on the
name
column.– Madhur Bhaiya
Nov 11 at 10:40
I was thinking more from the Python perspective, code optimization for execution - not SQL. Sorry for misunderstanding. I clarified in more details this now
– Dejan
Nov 11 at 10:43