473,410 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

Connection not defined Error in the program

1
Dear All,
I have a problem while data logging in a single table in SQL DB. We are not able to do it. When we are running the python code in raspberry pi we are getting the following error msg.

Program:
Expand|Select|Wrap|Line Numbers
  1. import serial
  2. import time
  3. import MySQLdb as mdb
  4.  
  5. arduino = serial.Serial("/dev/ttyACM0")
  6. arduino.baudrate=9600
  7.  
  8. data = arduino.readline()
  9. time.sleep(1)
  10. data = arduino.readline()
  11. pieces = data.split("\t")
  12.  
  13. temperature = pieces[0]
  14. humidity = pieces[1]
  15.  
  16. con = mdb.connect('localhost', 'root', '12345678', 'testdb');
  17.  
  18. with con:
  19.  
  20.     cursor = connection.cursor(prepared=True)
  21.     sql_insert_query = """ INSERT INTO TempDB (temperature, humidity) VALUES (?,?)"""
  22.  
  23.     insert_tuple = (temperature, humidity)
  24.     cursor.execute(sql_insert_query, insert_tuple)
  25.     connection.commit()
Error Message

Traceback (most recent call last):
File "./insertDB.py", line 20, in <module>
cursor = connection.cursor(prepared=True)
NameError: name 'connection' is not defined
Jun 23 '20 #1
4 21856
Banfa
9,065 Expert Mod 8TB
I'm not a Python expert but it looks to me like you added the with con: and forget to remove the connection. at lines 20 and 25.
Jun 23 '20 #2
AjayGohil
83 64KB
Hi,

You use con variable to store connection string and and use connection variable. so,use con instead of connection.
Jun 24 '20 #3
shkum
1 Bit
I got same error when tried to connect to DB wich was not yet created
Aug 24 '22 #4
sandrahdes
1 Bit
When we try to access a variable or function that is not defined or before it is defined, Python throws the error "NameError: name is not defined." Make sure the variable's name is spelled correctly and access it after it has been defined to fix the issue. Here is an illustration of the fallacy in action. When a variable, function, or module is used in Python but either doesn't exist or wasn't used properly, a NameError is raised. Common errors that lead to this error include the following: using a name for a variable or function that hasn't yet been declared.
Sep 2 '22 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: beav At wn DOT com DOT au | last post by:
Students at the school where I work are getting the error "Application-defined or object-defined error." when trying to document their Access databases. If you open the documenter window, select a...
6
by: Chris Szabo | last post by:
I've created a data access layer for a .NET web application. I'm using C# and framework 1.1. I'm getting an error from time to time when I close a connection saying: ...
0
by: May | last post by:
Hello, We got this error (Internal connection fatal error) from our application and at the same we got "server application unavailable" error on the browser. I looked at the line that was...
5
by: Henry | last post by:
I am writing a Windows forms VB.Net/MS SQL application that utilizes Crystal Reports. I want to be able to dynamically set the report data source at run time. According to article...
1
by: Sylaris | last post by:
hi, i have a problem which recursion fits perfectly, however; after working with the base function (which has no errors and works correctly) the recursions return a "function not defined" error in...
0
by: ewarts | last post by:
Hello all, I keep getting the following error whenever i execute a access program. The program is supposed to retrieve data from the source database and then put it into an excel spreadsheet then...
2
by: LeoK | last post by:
After finally completing my database I was ready to transfer it to our client, but when the access database was opened, everything works great, except for the SEND_EMAIL part, whenever any code that...
7
by: JFKJr | last post by:
Hi! I am a new beginner to MS Access VBA, I wrote the following VBA code in Access which deletes blank columns and rows in excel file. But, the code works fine for sometimes and sometimes it displays...
0
by: Hema Suresh | last post by:
Hi all I am trying to create a database using vb6 and excel I created few controls on the vb form and coded to get the details from the user I code in the way to pass the data which i got from the...
6
topher23
by: topher23 | last post by:
I have an Access subroutine that needs to generate an Excel spreadsheet for accounting. I've used late-bound Excel automation before, but for some reason I keep running into a strange issue when I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.