473,563 Members | 2,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Connection not defined Error in the program

1 New Member
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.curs or(prepared=Tru e)
NameError: name 'connection' is not defined
Jun 23 '20 #1
4 21990
Banfa
9,065 Recognized Expert Moderator Expert
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 New Member
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 New Member
I got same error when tried to connect to DB wich was not yet created
Aug 24 '22 #4
sandrahdes
1 New Member
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
4734
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 table and click OK, it works. If you click options and then click OK the above error message comes up. When logged in as administrator, the...
6
2334
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: System.InvalidOperationException: A connection pooling error has occurred. It doesn't happen each time, the exception is thrown randomly. The big problem is that the...
0
1428
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 provided by the stack trace and it appeared that it happened when it tries to connect to the database to fill dataset. What might cause the database...
5
12820
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 "cr_rdc9_connectionproperties.pdf" on the Crystal support web site I should be able to do the following: Dim ConnectionInfo As CRAXDRT.ConnectionProperties I...
1
17560
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 ff's js console. i tried a few things that came to mind but nothing has changed it... i found some interesting articles on the net regarding...
0
1264
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 replace it into a new excel spreadsheet which is a formatted sheet. Can anyone assist. Here is the error below in bold, the second line is the line...
2
8804
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 has to do with opening outlook I get a error, . I panic, so I try it on another computer and the same thing happened. After further investigation I...
7
13443
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 "application defined or object defined error". And, when I step into the code, error is occuring at the following Bold line. Please help me with...
0
1632
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 user to an excel sheet I named a function in a module as "Appending" and i made it to receive all the data through arguments And almost I coded...
6
1369
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 run this one. This is the relevant portion of the procedure: Private Sub GenerateExplodedBOM() 'On Error GoTo Err_Handler Dim appXL As Object...
0
7579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7877
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8101
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7631
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7943
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6238
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3631
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2077
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.