Python Forum
Attendence System - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Attendence System (/thread-1189.html)

Pages: 1 2


RE: Attendence System - nilamo - Dec-20-2016

You should post some more code. It looks like you haven't connected to the database yet.


RE: Attendence System - elliott0184 - Dec-20-2016

Ok didnt know how much i should post. The Mysql server is on the same machine i am using to write the code.

import time


import mysql.connector


from mysql.connector import errorcode


from datetime import datetime


try:
   cnx = mysql.connector.connect(user='root',database='staff', host='localhost')
   
except mysql.connector.Error as err:
   if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
       print("Something is wrong with your username or password")
   elif err.errno == errorcode.ER_BAD_DB_ERROR:
       print("Database does not exist")
   else:
       print(err)
else:
   cnx.close()

cursor = cnx.cursor()
Thats when i receive the error.
Error:
--------------------------------------------------------------------------- OperationalError                          Traceback (most recent call last) <ipython-input-6-23c84600e42c> in <module>() ----> 1 cursor = cnx.cursor() /home/Elliott/Documents/ana/lib/python3.5/site-packages/mysql/connector/connection.py in cursor(self, buffered, raw, prepared, cursor_class, dictionary, named_tuple)   1381             raise errors.InternalError("Unread result found.")   1382         if not self.is_connected(): -> 1383             raise errors.OperationalError("MySQL Connection not available.")   1384         if cursor_class is not None:   1385             if not issubclass(cursor_class, CursorBase): OperationalError: MySQL Connection not available.



RE: Attendence System - wavic - Dec-20-2016

Do you have privileges to connect to the db?


RE: Attendence System - elliott0184 - Dec-20-2016

(Dec-20-2016, 06:30 PM)wavic Wrote: Do you have privileges to connect to the db?

I think i do how would i check from the mysql console? + shouldnt it give me a error if i couldn't on the line above?


RE: Attendence System - nilamo - Dec-20-2016

Try pinging the server after you've got a connection, but before trying to make a cursor. That way you can make sure the database is running and you're connected. https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlconnection-ping.html

You could also skip the cursor and use the connection directly with .cmd_query(): https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlconnection-cmd-query.html


RE: Attendence System - elliott0184 - Dec-21-2016

(Dec-20-2016, 06:41 PM)nilamo Wrote: Try pinging the server after you've got a connection, but before trying to make a cursor.  That way you can make sure the database is running and you're connected.  https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlconnection-ping.html  

You could also skip the cursor and use the connection directly with .cmd_query(): https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlconnection-cmd-query.html

ok so i tried to ping the server and i still gives me a big error. NEW code
import time


import mysql.connector


from mysql.connector import errorcode


from datetime import datetime


try:
   cnx = mysql.connector.connect(user='root', password='password', database='staff', host='localhost')
   
except mysql.connector.Error as err:
   if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
       print("Something is wrong with your username or password")
   elif err.errno == errorcode.ER_BAD_DB_ERROR:
       print("Database does not exist")
   else:
       print(err)
else:
   cnx.close()


cnx.ping(attempts=1,delay=0)
Then i get the error
Error:
OSError                                   Traceback (most recent call last) /home/Elliott/Documents/ana/lib/python3.5/site-packages/mysql/connector/network.py in send_plain(self, buf, packet_number)    129                 else: --> 130                     self.sock.sendall(packet)    131             except IOError as err: OSError: [Errno 9] Bad file descriptor During handling of the above exception, another exception occurred: OperationalError                          Traceback (most recent call last) /home/Elliott/Documents/ana/lib/python3.5/site-packages/mysql/connector/connection.py in ping(self, reconnect, attempts, delay)   1011         try: -> 1012             self.cmd_ping()   1013         except: /home/Elliott/Documents/ana/lib/python3.5/site-packages/mysql/connector/connection.py in cmd_ping(self)    874         """ --> 875         return self._handle_ok(self._send_cmd(ServerCmd.PING))    876 /home/Elliott/Documents/ana/lib/python3.5/site-packages/mysql/connector/connection.py in _send_cmd(self, command, argument, packet_number, packet, expect_response)    503                 self._protocol.make_command(command, packet or argument), --> 504                 packet_number)    505         except AttributeError: /home/Elliott/Documents/ana/lib/python3.5/site-packages/mysql/connector/network.py in send_plain(self, buf, packet_number)    132                 raise errors.OperationalError( --> 133                     errno=2055, values=(self.get_address(), _strioerror(err)))    134             except AttributeError: OperationalError: 2055: Lost connection to MySQL server at 'localhost:3306', system error: 9 Bad file descriptor During handling of the above exception, another exception occurred: InterfaceError                            Traceback (most recent call last) <ipython-input-6-ee7505b13fb4> in <module>() ----> 1 cnx.ping(attempts=1,delay=0) /home/Elliott/Documents/ana/lib/python3.5/site-packages/mysql/connector/connection.py in ping(self, reconnect, attempts, delay)   1015                 self.reconnect(attempts=attempts, delay=delay)   1016             else: -> 1017                 raise errors.InterfaceError("Connection to MySQL is"   1018                                             " not available.")   1019 InterfaceError: Connection to MySQL is not available.
Sorry for the big errors and for me being not very good at python. :) I have learned loads over the last few days and i thank you all for your help.


RE: Attendence System - Larz60+ - Dec-21-2016

Looks to me like the server is not running


RE: Attendence System - elliott0184 - Dec-21-2016

(Dec-21-2016, 01:36 PM)Larz60+ Wrote: Looks to me like the server is not running

I can access the server from the console though. i start the server from the terminal and then i can access it. I presume that means it is up?


RE: Attendence System - elliott0184 - Dec-21-2016

Ok problem solved. I started up a apache server on the same PC and it seemed to fix it. Don't really
Understand but it now works. Thanks very much.


RE: Attendence System - elliott0184 - Feb-21-2017

Ok so thought I would use the same thread even though I am much further on. Now I am looking at the clocking out section. I have managed to get the system to get them to input their staff id twice. it then goes to check the server. if it finds the right code in one of the MYSQL tables it returns saying clocked it. It then adds a row to a different table with the date in one column, the "time in" in another and their name in the last. 

How do I get it to check if they are already clocked in that day and if they are, clock them out by adding a time into the clock out column.

Any help would be great thanks