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 7194909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:28:16+00:00 2026-05-28T20:28:16+00:00

I’m very new to python and I have a basic python script where I

  • 0

I’m very new to python and I have a basic python script where I need to insert values from a CSV file into an SQLite database using Python. I’ve done that part successfully, however, I need to not include columns 26, 27 and 28 out of 29. Whenever I try to do this, it just skips over the entire row, however I only want it to skip that specific column in general. I tried to not include the columns when creating the table originally however, I would get an error each time when I ran the script

This is the code I’m working with with regards to this process:

import sys, sqlite, csv

  try: 
    cx = sqlite.connect("database")

except sqlite.Error, errmsg:
    print "Could not open the database file: " + str(errmsg)
    sys.exit()

#create the table        
try:
    cu = cx.cursor()
    cu.execute('DROP TABLE IF EXISTS table_name')

    sql = """ CREATE TABLE table_name(col1 TEXT, col2 TEXT, col3 TEXT, col4 TEXT, col5 TEXT, col6 TEXT, col7 TEXT, col8 TEXT, col9 TEXT, col10 TEXT, col11 TEXT, col12 TEXT, col13 TEXT, col14 TEXT, col15 TEXT, col16 TEXT, col17 TEXT, col18 TEXT, col19 TEXT, col20 TEXT, col21 TEXT, col22 TEXT, col23 TEXT, col24 TEXT, col25 TEXT, col26 TEXT, col27 TEXT, col28 TEXT, col29 TEXT); """

    cu.execute(sql)
    cx.commit()
except sqlite.Error, errmsg:
    print "Could not execute the query: " + str(errmsg)
    sys.exit()

#Load the CSV file into the csv reader


fin = open("test.csv", "rb")
creader = csv.reader(fin, delimiter=',')

# Interate through the CSV Reader, inserting each value into the database
    # NEW REVISION
  sql_insert = "INSERT INTO table_name VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,,,,%s);"    


      for row in creader:
         cu.execute(sql_insert, row)
         print row

fin.close()
cx.commit()
cx.close()
  • 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-05-28T20:28:17+00:00Added an answer on May 28, 2026 at 8:28 pm

    You are iterating over the output of csv.reader() as if they are columns. That is incorrect. They are in fact whole rows. It should be more like:

    for row in reader:
        # Now to skip the mentioned columns just don't use those in your sql insert statement
        # for example column 25 is row[24]
        # Instead, put a blank in your SQL
        row[25]=""
        row[26]=""
        row[27]=""
    
        sql_insert = "INSERT INTO table_name   VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"  
        cursor.execute(sql_insert , row)
    

    insert instead of %s for those columns

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I have thousands of HTML files to process using Groovy/Java and I need to

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.