LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-16-2007, 12:37 PM   #1
talat
Member
 
Registered: Jan 2006
Distribution: Centos
Posts: 145

Rep: Reputation: 16
Question MYSQL table updation


Hi Guys

I have a MYSQL database on my Linux box. This database has one table which has following structure.

mysql> describe data;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| S.No. | int(5) | NO | PRI | NULL | auto_increment |
| ID | varchar(255) | YES | | NULL | |
| PASS | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

Now in this table there are many rows which have password value equal to NULL. I have a file which is generated from a log file and this file has decoded password values in the following format.
i.e
emailid pass

e.g
talat@xyz.com talat
abc@xyz.com abc123

Can some one tell me how can i update my table via some script (or any other possible way) by first searching the the email id (from table) of password NULL in the file and then update its password column value.

Thanks in advance.


Regard
Talat
 
Old 10-16-2007, 12:47 PM   #2
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Rep: Reputation: 31
You could use statements something like:

UPDATE data SET PASS='talat' WHERE ID='talat@xyz.com' AND PASS IS NULL;

A simple way without having to write a script, is to copy your text file and paste into OpenOffice spreadsheet. Then you can swap the two columns so that pass is before the email. Next you can add a column before with "UPDATE data SET PASS='" as the text, add a column in the middle with "' WHERE ID='" and finally a column at the end with "' AND PASS IS NULL;"

Then if you copy/paste that into gedit, you'll have an SQL script you can run from mysql.

Not the most elegant way, but works if you aren't able to quickly whip up a script.
 
Old 10-16-2007, 03:06 PM   #3
talat
Member
 
Registered: Jan 2006
Distribution: Centos
Posts: 145

Original Poster
Rep: Reputation: 16
Dear MicahCarrick,

Thanks for the reply ,but the situation is like this. There are many unique email ids in the table some have their passwords and some ids passwords are NULL (unknown). From the file(separate file) which is extracted from log file contain many line (repetitive also) from them if any of the email id of our DB table occurs with its password them i have to update the table and update its password column. Following is the example of table and the file values.

Table

S.NO id pass
1 talat@xyz.com talat
2 abc@xyz.com abc123
3 shane@xyz.com NULL
4 alex@xyz.com alex
etc

File

talat@xyz.com talat
alex@xyz.com alex
talat@xyz.com talat
talat@xyz.com talat
shane@xyz.com shane123
etc

So far what i understand is this that i have to write a loop which pick the
first email id of password= Null ,then search that email id in the file if it exist in the file then pick the pass value and update the DB table pass column value and then move to next email id with pass=NULL .If that id is not found in the file then move to next email id.

But so far not been able to achieve it.

Regard
Talat
 
Old 10-16-2007, 03:24 PM   #4
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
Did I understand you right? There is no diiference whether the password is NULL or not and db can be updated without considering it (that the password is NULL)?
 
Old 10-16-2007, 03:46 PM   #5
talat
Member
 
Registered: Jan 2006
Distribution: Centos
Posts: 145

Original Poster
Rep: Reputation: 16
Hi Alien_Hominid

NULL is the value of the pass column for those ids whose passwords are unknown to me. So your right and i only have to update those rows whose password value is = Null.

Regard
Talat
 
Old 10-16-2007, 08:53 PM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
I would do it like this:

Import the file with all the e-mail password combinations into an additional table. (I hope that all equal e-mail addressed have the same passwords) Call this table email, use column names "mail" and "pass".

Then issue this SQL statement:

Code:
update data left join email on id=mail set data.pass=email.pass where isnull(data.pass);
Note that data.pass = NULL or data.pass = 'NULL' does NOT work, you can't match the NULL value, you have to use the isnull() function.

Be sure to have a backup of your database, one error and you wipe out all the passwords you already have!

In the future, PLEASE use [code] tags around your table spec. You get them by pressing the sharp symbol (#) on top of this window. I was not able to read your table specification.

jlinkels
 
Old 10-17-2007, 01:30 AM   #7
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
jlinkels sollution of course is more effective, but because there is no difference whether the password is already set or not (NULL), you can also do it without where condition:
Code:
update data left join email on id=mail set data.pass=email.pass
This will work provided that each user in the log file has provided correct password.
 
Old 10-17-2007, 05:38 AM   #8
Atif_Khan
Member
 
Registered: Sep 2004
Distribution: CentOS, Slackware, Solaris, Ubuntu
Posts: 66

Rep: Reputation: 15
Nice solution by jlinkels.
 
Old 10-17-2007, 08:54 AM   #9
talat
Member
 
Registered: Jan 2006
Distribution: Centos
Posts: 145

Original Poster
Rep: Reputation: 16
Gr8 idea guys Thanks

Regard
Talat
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL db table tommytomato Linux - General 10 08-24-2004 05:57 AM
MySQL: Can't use '(' or ')' in table name Mikessu Linux - Software 0 08-03-2004 03:59 AM
MySQL non-realtime table-by-table mirroring Passive Linux - Software 1 01-20-2004 12:11 PM
How to import MS ACCESS Table including OLE filed into the MySQL Table ? myunicom Linux - General 1 11-28-2003 11:30 AM
deleted mysql table in mysql now cant do anything nakkaya Linux - Software 0 03-18-2003 06:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration