Syntax error in SQlite Query,(Can't execute) - sql

class Assets:
def __init__(self):
conn = sqlite3.connect('FastFood.db')
c = conn.cursor()
self.Cash = input('Do you want to enter Cash:')
self.Equipment = input('Do you want to enter Eqiupment:')
self.Supplies = input('Do you want to enter Supplies:')
self.PrepaidInsurance = input('Do you want to enter Prepaid Insurance:')
self.ARecievale = input('Do you want to enter Accounts Recievable :')
query = c.execute('INSERT INTO Financial_Table (Account_Title)VALUES', (self.Cash))
result = conn.execute(query)
conn.close()
''' I am tring to insert entry into one column but it's giving a syntax error.'''
'''We have total 4 columns'''

Related

Cannot update SQL Boolean value

For my toggle of a Boolean value with Flask and SQLite I want to change 1 into 0 and 0 into 1 on table engineering_project. The message can be successfully flashed so the if function is working. However, the value of engg_proj_status in the table cannot be updated:
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute("SELECT engg_proj_status FROM engineering_project WHERE engg_proj_id =?",(engg_proj_id,))
status = c.fetchone()[0]
if status == 1:
c.execute("UPDATE engineering_project SET engg_proj_status = ? WHERE engg_proj_id = ?;",(False,engg_proj_id))
flash("Status changed from COMPLETED to OPEN")
else:
c.execute("UPDATE engineering_project SET engg_proj_status = ? WHERE engg_proj_id = ?;",(True,engg_proj_id))
flash("Status changed from OPEN to COMPLETED")
Add conn.commit()
Thanks to this comment from forpas.

Return the Data from Column Using PowerShell

When I export the $dt to a CSV - it shows in 1.A of Excel = 1 and in 2.A = 2. I need to extract the two value of the second row in column A. First why is the count working? And why do I not get a return for the values in the last two echo commands when I know there is a value = 2 in Row#2 - Column A?
$factory = [System.Data.Common.DbProviderFactories]::GetFactory('IBM.Data.DB2')
$cstrbld = $factory.CreateConnectionStringBuilder()
$cstrbld.Database = 'mydb'
$cstrbld.UserID = 'user'
$cstrbld.Password = 'userpass'
$cstrbld.Server = 'server:port#'
$dbconn = $factory.CreateConnection()
$dbconn.ConnectionString = $cstrbld.ConnectionString
$dbconn.Open()
$dbcmd = $dbconn.CreateCommand()
$dbcmd.CommandText = 'SELECT COUNT(*) FROM dbname.UNIT WHERE STATUS = 3'
$rdr = $dbcmd.ExecuteReader()
$dt = New-Object System.Data.DataTable
$dt.Load($rdr)
$dbconn.Close()
echo $dt.rows.count
## Returns 1
echo $dt.rows[1]
## Returns nothing
echo $dt.rows[1].column0
## Returns nothing
code ```
because index start to 0, try this:
$dt.rows[0]
$dt.Rows[0].ItemArray[0].ToString()

Efficient stats from Access DB

I am pulling stats from Access DB and using the following:
'''
countBP = Convert.ToInt32(New OleDbCommand(commandBP.ToString, con).ExecuteScalar)
countWP = Convert.ToInt32(New OleDbCommand(commandWP.ToString, con).ExecuteScalar)
countHP = Convert.ToInt32(New OleDbCommand(commandHP.ToString, con).ExecuteScalar)
'''
where:
'''
command = ""SELECT COUNT(*) FROM Employees WHERE Archived = 'N' AND ID > 2"
commandBP = command.ToString + " AND Ethnic = 'B' AND EmployeeType = 1"
commandWP = command.ToString + " AND Ethnic = 'W' AND EmployeeType = 1"
commandHP = command.ToString + " AND Ethnic = 'H' AND EmployeeType = 1"
'''
My question is; is this efficient? I am pulling 20+ stats separately, and it seems to be taking more and more time to load as the DB grows. I wondering if "SELECT * FROM Employees" to a dataset and then filter would be a better approach?
The only variable part of your query is the Ethnic field. This suggest to use the Group By clause on that field
command = "SELECT Ethnic, COUNT(*) FROM Employees
WHERE Archived = 'N' AND ID > 2 AND EmployeeType = 1
GROUP BY Ethnic"
Now this reduces the database calls to just one call and you can retrieve your data with
Dim data as Dictionary(Of String, Int32) = new Dictionary(Of String, Int32)()
OleDbCommand cmd = New OleDbCommand(command, con)
OleDbDataReader reader = cmd.ExecuteReader();
while(reader.Read())
data(reader.GetString(0)) = reader.GetInt32(1)
End While
At this point you can get the count values from your dictionary
Dim countBP as Integer
data.TryGetValue("B", countBP)
....
Notice that you should use the TryGetValue method to extract values from the database because if there are no record for Ethnic = "B" there will be no entry in the dictionary for the Key "B" but TryGetValue will leave the CountBP initialized with its default to zero.

show sql data from User input

The code below inserts whatever data is typed into the text fields: name1 and phone1 into my database and I need to be able to type in stored data and retrieve it
def insert():
name1 = textin.get()
phone1 = textinn.get()
conn = sqlite3.connect('D:\lastfm-dataset-360K\msd.sqlite3')
with conn:
cursor = conn.cursor()
cursor.execute('INSERT INTO people(name, phone) VALUES(?,?)',(name1, phone1,))
db.close()
but=Button(root,padx=2,pady=2,text='Submit',command=insert,font=('none 13 bold'))
but.place(x=60,y=100)
I need to retrieve the records by typing them into the same text field and then print them out. So far I have this but Im confused with the SQL.
def show():
name1 = textin.get()
phone1 = textinn.get()
conn = sqlite3.connect('D:\lastfm-dataset-360K\msd.sqlite3')
with conn:
cursor = conn.cursor()
cursor.execute('SELECT * FROM people(name, phone) VALUES(?,?)',(name1, phone1,))
for row in cursor.fetchall():
print(row)
res=Button(root,padx=2,pady=2,text='Show',command=show,font=('none 13 bold'))
res.place(x=160,y=100)
Use:
cursor.execute('''INSERT INTO students(name, phone) VALUES(?,?)''',[(name1), (phone1)])
and:
cursor.execute("SELECT * FROM students WHERE name = ? AND phone = ?", [(name1),(phone1)])
Newcode:
def insert():
name1 = textin.get()
phone1 = textinn.get()
conn = sqlite3.connect('D:\lastfm-dataset-360K\msd.sqlite3')
with conn:
cursor = conn.cursor()
cursor.execute('''INSERT INTO students(name, phone) VALUES(?,?)''',[(name1), (phone1)])
db.close()
def show():
name1 = textin.get()
phone1 = textinn.get()
conn = sqlite3.connect('D:\lastfm-dataset-360K\msd.sqlite3')
with conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM students WHERE name = ? AND phone = ?", [(name1),(phone1)])
for row in cursor.fetchall():
print(row)
res=Button(root,padx=2,pady=2,text='Show',command=show,font=('none 13 bold'))
res.place(x=160,y=100)

Rails : Calling a function in a loop returns same value for each iteration

I am trying to calculate Leave taken by a particular student "UG10001" in a particular month from a leaves table in mysql database. I am using following snippets of code--
def calculate(student_id)
leave = Leave.find_by_sql("SELECT leave_duration FROM leaves WHERE MONTH(leave_from) = 2
AND YEAR(leave_from) = 2013 AND MONTH(leave_to) = 2 AND YEAR(leave_to) = 2013 AND
academic_status = 'APPROVED' AND warden_status = 'APPROVED' AND student_id = student_id
AND status = 'ACTIVE'")
sum = leave.sum(&:leave_duration)
return sum
end
----------Update----------
def calculate(student_id)
sum = Leave.find_by_sql("SELECT leave_duration FROM leaves WHERE MONTH(leave_from) = 2
AND YEAR(leave_from) = 2013 AND MONTH(leave_to) = 2 AND YEAR(leave_to) = 2013 AND
academic_status = 'APPROVED' AND warden_status = 'APPROVED' AND student_id = student_id
AND status = 'ACTIVE'").sum(&:leave_duration)
return sum
#it did the trick for me.
end
The above method will calculate the leave taken by a particular student in the month of February '13 and returns the sum of the leaves to the calling function.
def calcuateLeaveForEachStudent
array = Leave.find_by_sql("select student_id from students")
c = Array.new
for i in 0...array.length
student_id = array[i].student_id
c[i] = calculate(student_id)
end
end
But when I call the above method, 'calculate' method returns the same value on each iteration. What could be the possible solution for this..? Thanks in advance..!
PS -
Code runs perfectly on Rails Console without any syntactical errors, however there is some error I can't figure out.
calculate(operation, column_name, options = {}).
or you can refer the link