Page 1 of 1

terminal chat program

Posted: Fri Sep 16, 2011 8:20 am
by Isaac
I will do the challenges after this. I really had to get a good introduction to MySQL going first.

Check it out. I made a terminal chat program. It's kinda silly, because its a terminal based chat program, that only updates when you make a post. Kinda weird. But it works. Saves to my msql table and everything.

What do you all think?

Code: Select all

import MySQLdb


class chatsys:
	def __init__(self):
		self.newcomment=""
		chat = MySQLdb.connect( host = "localhost" , user="isaac" , passwd="password" , db="chatbox" )
		self.cur = chat.cursor()
	def login(self):
		self.username=raw_input("User name?")
	def chat(self):
		self.comment=raw_input("Comment: (Type 'exit' to exit) ")
		self.newcomment="insert into chatlog values(NULL,'%s',NULL,'%s')" % (self.username,self.comment)
		self.cur.execute(self.newcomment)
	def showlog(self):
		self.cur.execute("select * from chatlog ORDER by -id limit 15")		
		lastfive=[a for a in self.cur.fetchall()]
		lastfive.reverse()
		for a in lastfive:
			print str(a[0])+":  "+str(a[1])+" Said: "+str(a[3])
		
c=chatsys()
c.login()

while True:
	
	c.showlog()
	c.chat()
	if c.comment == "exit":
		print "exited"
		break



c.cur.close()
edit:
Oops. Forgot to show my table description:

Code: Select all


mysql> describe chatlog;
+----------+--------------+------+-----+-------------------+-----------------------------+
| Field    | Type         | Null | Key | Default           | Extra                       |
+----------+--------------+------+-----+-------------------+-----------------------------+
| id       | mediumint(9) | NO   | PRI | NULL              | auto_increment              |
| USERNAME | char(30)     | YES  |     | NULL              |                             |
| TIMELOG  | timestamp    | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| comment  | varchar(300) | YES  |     | NULL              |                             |
+----------+--------------+------+-----+-------------------+-----------------------------+

Re: terminal chat program

Posted: Fri Sep 16, 2011 10:01 am
by fliptw
terminal chat programs are pretty old software.

Re: terminal chat program

Posted: Fri Sep 16, 2011 12:43 pm
by Jeff250
Does this work when the user attempts to chat:

Code: Select all

foo'bar
?
Hint: prepared statements.

Re: terminal chat program

Posted: Fri Sep 16, 2011 2:30 pm
by Isaac
Jeff250 wrote:Does this work when the user attempts to chat:

Code: Select all

foo'bar
?
Hint: prepared statements.
Omg. hahah wow. Nope! It does not. Thanks.