Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8544619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:36:15+00:00 2026-06-11T12:36:15+00:00

Simple question that probably has a very simple answer. I am writing date strings

  • 0

Simple question that probably has a very simple answer. I am writing date strings of the format “2012-06-10” to a TEXT column from a Python script.

e.g.

cur.execute("CREATE TABLE tbl(date TEXT, ...)")

cur.execute('INSERT INTO tbl VALUES(%s)' % (str(date[i])), ...)

The script is actually evaluating the date string, so “2012-06-10” gets written into the table as “1996”. From reading the docs, I’m guessing this has something to do with type affinity, but I can’t figure how to override or even why the string would be evaluated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:36:17+00:00Added an answer on June 11, 2026 at 12:36 pm

    Two ways:

    • By using a parameterized query: cur.execute("INSERT INTO tbl VALUES (?), [str(date[i])]). This is the best way. Do it this way. The second method is only being included for posterity.
    • By quoting the value (note: this is almost certainly in almost every case the wrong way): cur.execute("INSERT INTO tbl VALUES ('%s')" %(str(date[i]), ). This method is suboptimal because, unless you’re careful, you’ll be vulnerable to SQL injection.

    To understand why this is going on, imagine the query that your code is sending to SQLite:

     INSERT INTO tbl VALUES (2012-06-10);
    

    Which the SQL engine correctly evaluates to:

     INSERT INTO tbl VALUES (1996);
    

    Quoting the value will solve this issue:

     INSERT INTO tbl VALUES ('2012-06-10');
    

    But can lead to issues if the value has certain characters in it (characters like ' or the null byte).

    However, with a parameterized query, the values are sent separately from the query, so there is no chance they will be misinterpreted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a question that probably has a pretty simple answer, but I didn't
All, This question probably has a very simple answer - something I'm overlooking. But
This question probably has a very simple answer/formula but math is not a strong
I have a question that is probably very simple, but I cannot find an
This is probably a very simple question that I am working through in an
There is probably a very simple answer to this question, but I can't for
This is probably a very simple question to answer. i am new to Rails
This is probably a basic html/css question... I have a simple one-button form that
I have a relatively simple question that I cannot seem to find the answer
Simple question - How do I convert a string that has been through parseInt

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.