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










share|improve this question
























  • 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










  • 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

















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










share|improve this question
























  • 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










  • 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















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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 10:54

























asked Nov 11 at 10:37









Dejan

54982147




54982147












  • 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










  • 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










  • 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


















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














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.






share|improve this answer





















  • 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










  • Do mention such constraints before hand itself
    – swapnil shashank
    Nov 11 at 11:53













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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.






share|improve this answer





















  • 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










  • Do mention such constraints before hand itself
    – swapnil shashank
    Nov 11 at 11:53

















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.






share|improve this answer





















  • 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










  • Do mention such constraints before hand itself
    – swapnil shashank
    Nov 11 at 11:53















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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 11:03









swapnil shashank

625




625












  • 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










  • 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










  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python