0

I have a table in a database with columns, dept name, name, id, date,day, time, mobile number. Each column is having its own value. I want to fetch the above data from the database and send an sms extracting details from the database table The sms format is :the attendance of name on date, day is:time (send to no in database)

we found the code for sending sms to text file and its working fine through sim 300. but we want it to send to nos. stored in database the code we designed for text file is as follows

import serial
import MySQLdb as mdb
import _mysql as m

def sendMsg(s,num,text):
    cr = chr(13)
    s.write('at' + cr)
    s.write('AT+CMGF=1'+cr)
    s.write('AT+CMGS="' + num + '"' + cr)
    s.write(text)
    s.write(cz)
    s.write(cr)
    print "Msg Sent"
    #print s.readline()
    #print s.readline()
#def sendComm(s,string):
#    s.write(string + chr(13))
#    print s.readline()
#    print string,'--->',s.readline()

s = serial.Serial(19)
cz = chr(26)
s.setBaudrate(9600)
with open('palav.txt','r') as f:
    lines = f.readlines()
    for line in lines:
        name,number,att =  line.split(',')
        att = att.strip()
        text = "Your ward %s is having attendance %s%%" % (name,att)
        print text
        sendMsg(s,number,text)
        print "Message Sent"


s.close()

please help us to send it through database username:root,password:"" database :attend

1 Answers1

0
#!/usr/bin/python
# view_rows.py - Fetch and display the rows from a MySQL database query
# import the MySQLdb and sys modules
import MySQLdb
import sys



# open a database connection
# be sure to change the host IP address, username, password and database name to match your own
connection = MySQLdb.connect (host = "192.168.1.2", user = "user", passwd = "password, db = "scripting_mysql")
# prepare a cursor object using cursor() method
cursor = connection.cursor ()
# execute the SQL query using execute() method.
cursor.execute ("select mobile_number from table ")
# fetch all of the rows from the query
data = cursor.fetchall ()
# print the rows
for row in data :
    print row[0]// this ur mobile no --> add this to ur function send sms

    // addd ur function here
# close the cursor object
cursor.close ()
# close the connection
connection.close ()
# exit the program
sys.exit()
sundar nataraj
  • 8,524
  • 2
  • 34
  • 46