Skip to content

Instantly share code, notes, and snippets.

@amrhamedp
Forked from pnavarro-algometrics/numpy-sqlite3.py
Created December 27, 2017 06:10
Show Gist options
  • Select an option

  • Save amrhamedp/c13eef0c95ff8743549745e278150e8b to your computer and use it in GitHub Desktop.

Select an option

Save amrhamedp/c13eef0c95ff8743549745e278150e8b to your computer and use it in GitHub Desktop.
Insert numpy array into a sqlite3 database
import sqlite3
import numpy
# Array of 4 columns and 100 rows
data = numpy.random.rand(100, 4)
# Create a sample database
conn = sqlite3.connect('/tmp/sample.db')
cursor = conn.cursor()
# Create a new table with four columns
cursor.execute('''create table data (field1 real, field2 real, field3 real, field4 real)''')
conn.commit()
# Insert the data array into the 'data' table
cursor.executemany('''insert into data values (?, ?, ?, ?)''', map(tuple, data.tolist()))
conn.commit()
cursor.close()
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment