There are several ways to log Python data into a database:
Example:
import sqlite3
# Connect to database
conn = sqlite3.connect('example.db')
c = conn.cursor()
# Insert data into table
c.execute(\"INSERT INTO my_table (column1, column2) VALUES ('data1', 'data2')\")
# Commit changes
conn.commit()
# Close database connection
conn.close()
Example:
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
# Define database model
Base = declarative_base()
class MyTable(Base):
__tablename__ = 'my_table'
id = Column(Integer, primary_key=True)
column1 = Column(String)
column2 = Column(String)
# Connect to database and create session
engine = create_engine('sqlite:///example.db')
Session = sessionmaker(bind=engine)
session = Session()
# Insert data into table
data = MyTable(column1='data1', column2='data2')
session.add(data)
session.commit()
# Close session
session.close()
Example:
from pymongo import MongoClient
# Connect to MongoDB and get collection
client = MongoClient('mongodb://localhost:27017/')
db = client['my_database']
collection = db['my_collection']
# Insert data into collection
data = {'column1': 'data1', 'column2': 'data2'}
collection.insert_one(data)
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-06-13 11:00:00 +0000
Seen: 22 times
Last updated: Apr 10 '23
What is the process of integrating API data into MongoDB using Spark/Python?
How can data be extracted from Azure DocumentDB for exporting purposes?
How can a web application be developed to send distinct emails upon clicking the SUBMIT button?
What is the difference between indexing in Elasticsearch and MongoDB?
Where does my Springboot application load its database from?
Can specific items be eliminated from Parquet and NoSQL Targets?
In MongoDB Python, what is the technique for getting a key in a document as a value?
"How can I establish a connection between Python and MongoDB on Azure VM?"
What is the problem with the SSL Handshake when using Pymongo on Python3?