I used some very simple code to create a database with peewee, I am new to using python ORMs so I can't really tell why I'm getting a whole bunch of errors.
What this code does is: First I create a database 'diary.db'
the data types used are entries, which is a Text Field, and date, which is a DateTimeField. I created some functions: 'initialize' to run basic commands and initialize the database, 'menu_loop' that will show a menu that works with an infinite loop and may call the function 'add_entry' that adds new entries to the database.
Heres the code:
#!/usr/bin/env python3
from collections import OrderedDict
from peewee import *
import datetime
import sys
db = SqliteDatabase('diary.db')
class Diary(Model):
entries = TextField()
date = DateTimeField(default = datetime.datetime.now)
class Meta:
database = db
def initialize():
"""initializes the database"""
db.connect()
db.create_tables([Diary], safe = True)
#end of initialize
def menu_loop():
"""show menu"""
choice = 0
while choice != 2:
print("Enter '2' to quit")
print('1) to add an entry')
choice = input()
if choice == 1:
add_entry()
#end of menu_loop
def add_entry():
"""add an entry"""
print("Enter your entry, press ctrl+d when done")
data = sys.stdin.read().strip()
if data:
while(True):
option = input('\nSave entry?[1 = yes 0 = no] ')
if option == 1:
Diary.create(content=data)
print ("Saved sucessfully!")
if option == 0:
print ("Program exited")
break;
#end of add_entry
if __name__ == '__main__':
initialize()
menu_loop()
and the error log
Enter '2' to quit
1) to add an entry
1
Enter your entry, press ctrl+d when done
this is my new entry
hello world^D
Save entry?[1 = yes 0 = no] 1
Traceback (most recent call last):
File "ispythonsmart.py", line 50, in <module>
menu_loop()
File "ispythonsmart.py", line 30, in menu_loop
add_entry()
File "ispythonsmart.py", line 41, in add_entry
Diary.create(content=data)
File "/Library/Python/2.7/site-packages/peewee.py", line 4494, in create
inst.save(force_insert=True)
File "/Library/Python/2.7/site-packages/peewee.py", line 4680, in save
pk_from_cursor = self.insert(**field_dict).execute()
File "/Library/Python/2.7/site-packages/peewee.py", line 3213, in execute
cursor = self._execute()
File "/Library/Python/2.7/site-packages/peewee.py", line 2628, in _execute
return self.database.execute_sql(sql, params, self.require_commit)
File "/Library/Python/2.7/site-packages/peewee.py", line 3461, in execute_sql
self.commit()
File "/Library/Python/2.7/site-packages/peewee.py", line 3285, in __exit__
reraise(new_type, new_type(*exc_args), traceback)
File "/Library/Python/2.7/site-packages/peewee.py", line 3454, in execute_sql
cursor.execute(sql, params or ())
peewee.IntegrityError: NOT NULL constraint failed: diary.entries
You need to set entries to null=True or use a default value for entries:
class Diary(Model):
entries = TextField(null=True)
Output:
Enter '2' to quit
1) to add an entry
1
Enter your entry, press ctrl+d when done
foobar
Save entry?[1 = yes 0 = no] 1
Saved successfully!
You want to see "entries" TEXT in the db not "entries" TEXT NOT NULL. If a column is set to NOT NULL you must insert a value or you will get an integrity error, an alternative is to give a default value for the column i.e TextField(default="foo"). On a sidenote you have #!/usr/bin/env python3 as your shebang but your code is written for python 2 so you may want to correct that.
Related
`
import streamlit as st
from streamlit_option_menu import option_menu
import json
from streamlit_lottie import st_lottie
import pickle
import requests
from db1 import insert_review, fetch_all_reviews, get_movie_review
movies = pickle.load(open('movie_list.pkl', 'rb'))
similarity = pickle.load(open('similarity.pkl', 'rb'))
`
`
if selected == "Review":
st.title("Reviews")
with st.form("my_form"):
st.write("Type your Review")
user_name_rev = st.text_input("User Name")
movie_list = movies['title'].values
selected_movie = st.selectbox(
"Type or select a movie from the dropdown",
movie_list
)
slider_val = st.slider("Rating", 1, 10)
# st.write(f"{slider_val}")
movie_rev = st.text_area("Write your review")
#checkbox_val = st.checkbox("Form checkbox")
submitted = st.form_submit_button("Submit")
if "load_state" not in st.session_state:
st.session_state.load_state = False
if submitted or st.session_state.load_state:
st.session_state.load_state = True
user = str(st.session_state[user_name_rev])
movies = str(st.session_state[selected_movie])
rate = st.session_state[slider_val]
review = str(st.session_state[movie_rev])
insert_review(user, movies, rate, review)
st.write("Your review is saved")
I was trying to save the data in db and whenever I click submit it shows error.
KeyError: 'st.session_state has no key "yy". Did you forget to initialize it? More info: https://docs.streamlit.io/library/advanced-features/session-state#initialization' Traceback: File "c:\users\yaagik\pycharmprojects\pythonproject\venv\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 564, in _run_script exec(code, module.__dict__) File "C:\Users\YAAGIK\PycharmProjects\pythonProject\new_test.py", line 77, in <module> user = str(st.session_state[user_name_rev]) File "c:\users\yaagik\pycharmprojects\pythonproject\venv\lib\site-packages\streamlit\runtime\state\session_state_proxy.py", line 89, in __getitem__ return get_session_state()[key] File "c:\users\yaagik\pycharmprojects\pythonproject\venv\lib\site-packages\streamlit\runtime\state\safe_session_state.py", line 110, in __getitem__ return self._state[key] File "c:\users\yaagik\pycharmprojects\pythonproject\venv\lib\site-packages\streamlit\runtime\state\session_state.py", line 439, in __getitem__ raise KeyError(_missing_key_error_message(key))
I tried to fix it with various methods available online but none of them worked for me.`
The problem is that you call 'st.session_state[user_name_rev]' on line 77 without initializing it.
To initialize a streamlit session_state you can do:
st.session_state['your_session_state_name'] = value_to_store
Or
if 'you_session_state_name' not in st.session_state:
st.session_state.you_session_state_name = value_to_store
In your specific case you can do:
if submitted or st.session_state.load_state:
if 'user' not in st.session_state:
st.session_state['user'] = user_name_rev
user = str(st.session_state['user'])
Do the same to movies, rate, review, this might work.
References:
https://docs.streamlit.io/library/advanced-features/session-state
"""
Created on Tue Sep 7 14:06:54 2021
#author: hp
"""
"""BOOK DETAILS IN SQL USING PYTHON----python librarian"""
import pandas as pd
import pyodbc
from sqlalchemy import create_engine,update
"""SERVER="DESKTOP-JKOITFK\SQLEXPRESS"
DATABASE="newdatabase"
DRIVER="SQL SERVER NATIVE CLIENT 11.0"
USERNAME="chitransh"
PASSWORD="reshushrey#2027"
#DATABASE_CONNECTION=f'mssql://{USERNAME}:{PASSWORD}#{SERVER}/{DATABASE}?driver={DRIVER}'"""
table_name="bookdetails"
engine=create_engine("mssql+pyodbc://#DESKTOP-JKOITFK\SQLEXPRESS/newdatabase?driver=SQL SERVER NATIVE CLIENT 11.0")
connection=engine.connect()
details={}
def bookdetails():
print("enter the book details:")
name=input("enter the name of the book:")
author=input("enter the author of the book:")
year=int(input("enter the year of the book:"))
publisher=input("enter the publisher of the book:")
quantities=int(input("enter the quantities of the book:"))
next_action=int(input("details entry completed. Press 1 for another entry, else press 2 for exit:"))
details={"BookName":[name],"Author":[author],"Year":[year],"Publisher":[publisher],"Quantities":[quantities]}
dict_df=pd.DataFrame(details)
#print(dict_df)
create_table=dict_df.to_sql(table_name,connection,if_exists="append",index=False)
if next_action==1:
bookdetails()
else:
authorized()
def issuebooks():
print("issue the books")
issue_book=input("which book to issue:")
frame=pd.read_sql("select Quantities from bookdetails where BookName = '{}'".format(issue_book),connection)
updated_value_query=(update(bookdetails).values(Quantities=(int(frame.values)-1)).where(bookdetails.BookName=='{}'.format(issue_book)))
connection.execute(updated_value_query)
def depositbooks():
print("deposit the books")
def authorized():
action=int(input("enter 1 for entering book details, enter 2 to issue books, enter 3 to deposit the book, enter 4 for exit:"))
if action==1:
bookdetails()
elif action==2:
issuebooks()
elif action==3:
depositbooks()
#else:
# main()
def enter_func(username,password):
if username not in librarian.keys():
print("you are not authorized to enter")
else:
if password==librarian[username]:
print("enter")
authorized()
else:
print("password donot match,try again")
#main()
while True:
first=int(input("press 1 to login, 2 for exit:"))
if (first==1):
librarian={"deepika":"chiku","pragya":"praveen"}
username=input("enter the username:")
password=input("enter the password:")
enter_func(username,password)
else:
break
I am try to make a book entry system and for that i am trying to connect SQL and python. When ever i try to update a value in SQL using update query, it shows the error
press 1 to login, 2 for exit:1
enter the username:deepika
enter the password:chiku
enter
enter 1 for entering book details, enter 2 to issue books, enter 3 to deposit the book, enter 4 for exit:2
issue the books
which book to issue:shiva2
Traceback (most recent call last):
File "<ipython-input-1-df831d64649f>", line 1, in <module>
runfile('C:/Users/hp/Desktop/project_part1.py', wdir='C:/Users/hp/Desktop')
File "C:\Users\hp\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\hp\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/hp/Desktop/project_part1.py", line 101, in <module>
enter_func(username,password)
File "C:/Users/hp/Desktop/project_part1.py", line 89, in enter_func
authorized()
File "C:/Users/hp/Desktop/project_part1.py", line 77, in authorized
issuebooks()
File "C:/Users/hp/Desktop/project_part1.py", line 61, in issuebooks
updated_value_query=update(bookdetails).values(Quantities=frame-1).where("BookName=='{}'".format(issue_book))
File "<string>", line 2, in update
File "C:\Users\hp\Anaconda3\lib\site-packages\sqlalchemy\sql\dml.py", line 735, in __init__
ValuesBase.__init__(self, table, values, prefixes)
File "C:\Users\hp\Anaconda3\lib\site-packages\sqlalchemy\sql\dml.py", line 201, in __init__
self.table = _interpret_as_from(table)
File "C:\Users\hp\Anaconda3\lib\site-packages\sqlalchemy\sql\selectable.py", line 49, in _interpret_as_from
raise exc.ArgumentError("FROM expression expected")
ArgumentError: FROM expression expected
This is the error which i am facing. It is saying FROM expression is expected, but when i write query in SQL, no FROM expression is written. I want to update the subtracted value in the SQL table.
I couldn't reproduce your exact error but this line
updated_value_query=(update(bookdetails).values(Quantities=(int(frame.values)-1)).where(bookdetails.BookName=='{}'.format(issue_book)))
needs a couple of changes to work:
the name bookdetails refers to the function bookdetails, so it isn't a valid argument for update, which expects a Table object or similar
in the where clause, the Quantities attribute must accessed through a table's columns or c attribute
import sqlalchemy as sa
...
# Create a table object that maps to the table in the database
bookdetails_table = sa.Table(table_name, sa.MetaData(), autoload_with=engine)
# Use the table object in the query
updated_value_query = (
update(bookdetails_table)
.values(Quantities=(int(frame.values) - 1))
.where(bookdetails_table.c.BookName == '{}'.format(issue_book))
)
You don't really need Pandas for your code, you could replace it with SQLAlchemy Core inserts and updates. For example (assuming SQLAlchemy 1.4+)
from sqlalchemy import create_engine, update
import sqlalchemy as sa
table_name = 'bookdetails'
engine = create_engine(...)
# Create a table and assign it to a global variable.
# Lowercase table and column names cause fewer problems than mixed or upper case
metadata = sa.MetaData()
book_table = sa.Table(
table_name,
metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('bookname', sa.String(128)),
sa.Column('author', sa.String(128)),
sa.Column('year', sa.Integer),
sa.Column('publisher', sa.String(128)),
sa.Column('quantities', sa.Integer),
)
book_table.create(engine, checkfirst=True)
def bookdetails():
print('enter the book details:')
name = input('enter the name of the book:')
author = input('enter the author of the book:')
year = int(input('enter the year of the book:'))
publisher = input('enter the publisher of the book:')
quantities = int(input('enter the quantities of the book:'))
next_action = int(
input(
'details entry completed. Press 1 for another entry, else press 2 for exit:'
)
)
details = {
'bookname': name,
'author': author,
'year': year,
'publisher': publisher,
'quantities': quantities,
}
insert = book_table.insert().values(**details)
with engine.begin() as conn:
conn.execute(insert)
if next_action == 1:
bookdetails()
else:
authorized()
def issuebooks():
print('issue the books')
issue_book = input('which book to issue:')
# Here we don't need to select and then update: we can express the update
# as an operation on the column.
updated_value_query = (
update(book_table)
.values(quantities=book_table.c.quantities - 1)
.where(book_table.c.bookname == issue_book)
)
with engine.begin() as conn:
conn.execute(updated_value_query)
I can't use sqlite function group_concat() in peewee. Here is complete snipet. Somehow peewee want to convert result of group_concat() to integer, while it is string ("1,2"). I can't find the way to suppress it.
from peewee import *
db = SqliteDatabase(':memory:')
class Test(Model):
name = CharField()
score = IntegerField()
class Meta:
database = db
db.create_tables([Test])
Test.create(name='A', score=1).save()
Test.create(name='A', score=2).save()
#select name, group_concat(score) from Test group by name
for t in Test.select(Test.name, fn.group_concat(Test.score)).order_by(Test.name):
pass
It produces following error:
Traceback (most recent call last):
File "C:\Users\u_tem0m\Dropbox\Wrk\sgo\broken.py", line 17, in <module>
for t in Test.select(Test.name, fn.group_concat(Test.score)).order_by(Test.name):
File "C:\Program Files\Python 3.5\lib\site-packages\peewee.py", line 1938, in next
obj = self.qrw.iterate()
File "C:\Program Files\Python 3.5\lib\site-packages\peewee.py", line 1995, in iterate
return self.process_row(row)
File "C:\Program Files\Python 3.5\lib\site-packages\peewee.py", line 2070, in process_row
setattr(instance, column, func(row[i]))
File "C:\Program Files\Python 3.5\lib\site-packages\peewee.py", line 874, in python_value
return value if value is None else self.coerce(value)
ValueError: invalid literal for int() with base 10: '1,2'
Try adding a coerce(False) to your call to group_concat:
query = (Test
.select(Test.name, fn.GROUP_CONCAT(Test.score).coerce(False))
.order_by(Test.name))
for t in query:
pass
Peewee sees that Test.score is an integer field, so whenever a function is called on that column, Peewee will try to convert the result back to an int. The problem is that group_concat returns a string, so we must tell Peewee not to mess with the return value.
Just found what result of fn.group_concat(""+Test.score) don't cast to integer. But I think resulting sql maybe less optimal
SELECT "t1"."name", group_concat(? + "t1"."score") AS allscore FROM "test" AS t1 ORDER BY "t1"."name" ['']
Do anybody knows more elegant way?
I have been trying to add items to a DynamoDB table using boto, but somehow it doesn't seem to work. I tried using users.Item() and users.put_item but nothing worked. Below is the script that I have in use.
import boto.dynamodb2
import boto.dynamodb2.items
import json
from boto.dynamodb2.fields import HashKey, RangeKey, GlobalAllIndex
from boto.dynamodb2.layer1 import DynamoDBConnection
from boto.dynamodb2.table import Table
from boto.dynamodb2.items import Item
from boto.dynamodb2.types import NUMBER
region = "us-east-1"
con = boto.dynamodb2.connect_to_region(region)
gettables = con.list_tables()
mytable = "my_table"
if mytable not in gettables['TableNames']:
print "The table *%s* is not in the list of tables created. A new table will be created." % req_table
Table.create(req_table,
schema = [HashKey('username'),
RangeKey('ID', data_type = NUMBER)],
throughput = {'read': 1, 'write': 1})
else:
print "The table *%s* exists." % req_table
con2table = Table(req_table,connection=con)
con2table.put_item(data={'username': 'abcd',
'ID': '001',
'logins':'10',
'timeouts':'20'
'daysabsent': '30'
})
I tried this, the table gets created and it is fine. But when I try to put in the items, I get the following error message.
Traceback (most recent call last):
File "/home/ec2-user/DynamoDB_script.py", line 29, in <module>
'daysabsent':'30'
File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/table.py", line 821, in put_item
return item.save(overwrite=overwrite)
File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/items.py", line 455, in save
returned = self.table._put_item(final_data, expects=expects)
File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/table.py", line 835, in _put_item
self.connection.put_item(self.table_name, item_data, **kwargs)
File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/layer1.py", line 1510, in put_item
body=json.dumps(params))
File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/layer1.py", line 2842, in make_request
retry_handler=self._retry_handler)
File "/usr/lib/python2.7/dist-packages/boto/connection.py", line 954, in _mexe
status = retry_handler(response, i, next_sleep)
File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/layer1.py", line 2882, in _retry_handler
response.status, response.reason, data)
boto.dynamodb2.exceptions.ValidationException: ValidationException: 400 Bad Request
{u'message': u'One or more parameter values were invalid: Type mismatch for key version expected: N actual: S', u'__type': u'com.amazon.coral.validate#ValidationException'}
Thank you.
From the error message you are getting, it sounds like you are trying to send string values for an attribute that is defined as numeric in DynamoDB.
The specific issue looks to be related to your Range Key ID which is defined as a numeric value N but you are sending it a string value '001'.
Looks like of of the values you are trying to load has empty value.
I got the same error when I was trying to load this. I got exception when partner_name property was a empty string.
try:
item_old = self.table.get_item(hash_key=term)
except BotoClientError as ex:
# if partner alias does not exist then create a new entry!
if ex.message == "Key does not exist.":
item_old = self.table.new_item(term)
else:
raise ex
item_old['partner_code'] = partner_code
item_old['partner_name'] = partner_name
item_old.put()
I try to use these both prepared statements in my django app:
READINGS = "SELECT * FROM readings"
READINGS_BY_USER_ID = "SELECT * FROM readings WHERE user_id=?"
I query against the db with:
def get_all(self):
query = self.session.prepare(ps.ALL_READINGS)
all_readings = self.session.execute(query)
return all_readings
def get_all_by_user_id(self, user_id):
query = self.session.prepare(ps.READINGS_BY_USER_ID)
readings = self.session.execute(query, [user_id])
return readings
The first of both works pretty well. But the second gives me:
ERROR 2015-07-08 09:42:56,634 | views::exception_handler 47 | ('Unable to complete the operation against any hosts', {<Host: localhost data1>: TypeError("'unicode' does not have the buffer interface",)})
Can anyone tell me what happened here? I understand, that there must be a unicode string somewhere that does not have a buffer interface. But which string is meant? My prepared statement?
Here is the stacktrace in addition:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rest_framework/views.py", line 448, in dispatch
response = handler(request, *args, **kwargs)
File "/Users/me/Workspace/project/Readings/views.py", line 36, in get_by_user_id
readings = self.tr_dao.get_all_by_user_id(user_id)
File "/Users/me/Workspace/project/Readings/dao.py", line 22, in get_all_by_user_id
readings = self.session.execute(query, [user_id], timeout=60)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cassandra/cluster.py", line 1405, in execute
result = future.result(timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cassandra/cluster.py", line 2967, in result
raise self._final_exception
if you are on python 2 this will probably fix it
def get_all_by_user_id(self, user_id):
query = self.session.prepare(ps.READINGS_BY_USER_ID)
readings = self.session.execute(query, [str(user_id)])
return readings
This is not working because your user_id is of type unicode. You can check it using
type(user_id)
If that is the case you should encode it to string:
str(user_id)
It will solve the issue.