Sagewire Logo

how to compare array of String with column of a table

7 Message(s) by 3 Author(s) originally posted in mysql discussion


From: kath Date:   Friday, October 26, 2007
Hi,
I have a array of string (say array is of length 1000). I want to
compare those string in array with one table column
- whether that table column has a string
if yes
don'thing.
if no
then insert that string into table.
- whether table has obsolete row i.e, the one present in table and
not in array
then delete that row.

How do I go about this, because I see, it isn't feasible to loop
through array and search table to find new string OR loop through each
row from table to find some obsolete row

How can I accomplish this task more feasibly(without running query for
each string, for comparission)? Is there any way to find this kind of
problem. I would've been easy if I had to compare two tables(with
UNION and INTERSECT), but it isn't the case.thanks,
kath.


From: Captain Paralytic Date:   Friday, October 26, 2007
wrote in message:
Hi, I have a array of string(say array is of length 1000). I want to
compare those string in array with one table column
- whether that table column has a string
if yes
don'thing.
if no
then insert that string into table.
- whether table has obsolete row i.e, the one present in table and
not in array
then delete that row.
How do I go about this, because I see, it isn't feasible to loop
through array and search table to find new string OR loop through each
row from table to find some obsolete row
How can I accomplish this task more feasibly(without running query for
each string, for comparission)? Is there any way to find this kind of
problem. I would've been easy if I had to compare two tables(with
UNION and INTERSECT), but it isn't the case.
thanks,
kath.



Load the array into a temporary table and use a MySQL intersect
construct (http://www.bit bybit.dk/carsten/blog/?p=71)


From: Rik Wasmus Date:   Friday, October 26, 2007
wrote in message:

Hi, I have a array of string(say array is of length 1000). I want to
compare those string in array with one table column
- whether that table column has a string
if yes
don'thing.
if no
then insert that string into table.
- whether table has obsolete row i.e, the one present in table and
not in array
then delete that row.
How do I go about this, because I see, it isn't feasible to loop
through array and search table to find new string OR loop through each
row from table to find some obsolete row
How can I accomplish this task more feasibly(without running query for
each string, for comparission)? Is there any way to find this kind of
problem. I would've been easy if I had to compare two tables(with
UNION and INTERSECT), but it isn't the case.



Aside from the temporary table solution given, this can also be done using
two queries:

0: set up table:
Give the field a UNIQUE index
1: delete:
DELETE FROM tablename WHERE fieldname NOT IN ('list ','of','values');
2: insert:
INSERT IGNORE INTO tablename (fieldname) VALUES ('list'),('of'),('values');
(allthough INSERT ... ON DUPLICATE KEY UPDATE ... could be more suited to
your needs)

From the manual:
If you use the IGNORE key word, error s that occur while executing the
INSERT statement are treated as warnings instead. For example, without
IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY
value in the table causes a duplicate-key error and the statement is
abort ed. With IGNORE, the row still isn't inserted, but no error is
issued. Data conversions that'd trigger errors abort the statement if
IGNORE isn't specified. With IGNORE, invalid values are adjusted to the
closest values and inserted; warnings are produced but the statement does
not abort. You can determine with the mysql_info() C API (application programming interface)function how many
rows were actually inserted into the table.

If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that'd
cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the
old row is performed. See Section 13.2.4.3, 'INSERT ... ON DUPLICATE KEY
UPDATE Syntax'. ON DUPLICATE KEY UPDATE was added in MySQL 4.1.0.
--
Rik Wasmus


From: Captain Paralytic Date:   Friday, October 26, 2007
wrote in message:
wrote in message:
> Hi,
> I have a array of string(say array is of length 1000). I want to
> compare those string in array with one table column
> - whether that table column has a string
> if yes
> don'thing.
> if no
> then insert that string into table.
> - whether table has obsolete row i.e, the one present in table and
> not in array
> then delete that row.
> How do I go about this, because I see, it isn't feasible to loop
> through array and search table to find new string OR loop through each
> row from table to find some obsolete row
> How can I accomplish this task more feasibly(without running query for
> each string, for comparission)? Is there any way to find this kind of
> problem. I would've been easy if I had to compare two tables(with
> UNION and INTERSECT), but it isn't the case.
Aside from the temporary table solution given, this can also be done using
two queries:
0: set up table:
Give the field a UNIQUE index
1: delete:
DELETE FROM tablename WHERE fieldname NOT IN ('list','of','values');
2: insert:
INSERT IGNORE INTO tablename (fieldname) VALUES ('list'),('of'),('values');
(allthough INSERT ... ON DUPLICATE KEY UPDATE ... could be more suited to
your needs)
From the manual:
If you use the IGNORE keyword, errors that occur while executing the
INSERT statement are treated as warnings instead. For example, without
IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY
value in the table causes a duplicate-key error and the statement is
aborted. With IGNORE, the row still isn't inserted, but no error is
issued. Data conversions that'd trigger errors abort the statement if
IGNORE isn't specified. With IGNORE, invalid values are adjusted to the
closest values and inserted; warnings are produced but the statement does
not abort. You can determine with the mysql_info() C API (application programming interface)function how many
rows were actually inserted into the table.
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that'd
cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the
old row is performed. See Section 13.2.4.3, 'INSERT ... ON DUPLICATE KEY
UPDATE Syntax'. ON DUPLICATE KEY UPDATE was added in MySQL 4.1.0.
--
Rik Wasmus- Hide quoted text -
- Show quoted text -



Depending on teh size of the table, I'd expect the NOT IN
('list','of','values') to be a bit slow (with 1000 values). I'd be
interesting to know the relative performance.


From: Rik Wasmus Date:   Friday, October 26, 2007
On Fri, 26 Oct 2007 16:27:48 +0200, Captain Paralytic
wrote in message:
wrote in message:
wrote in message:
> I have a array of string(say array is of length 1000). I want to
> compare those string in array with one table column
> - whether that table column has a string
> if yes
> don'thing.
> if no
> then insert that string into table.
> - whether table has obsolete row i.e, the one present in table and
> not in array
> then delete that row.


> How can I accomplish this task more feasibly(without running query for
> each string, for comparission)?

Aside from the temporary table solution given, this can also be done
using
two queries:

0: set up table:
Give the field a UNIQUE index
1: delete:
DELETE FROM tablename WHERE fieldname NOT IN ('list','of','values');
2: insert:
INSERT IGNORE INTO tablename (fieldname) VALUES
('list'),('of'),('values');
(allthough INSERT ... ON DUPLICATE KEY UPDATE ... could be more suited
to
your needs)

Depending on teh size of the table, I'd expect the NOT IN
('list','of','values') to be a bit slow (with 1000 values). I'd be
interesting to know the relative performance.



It'd certainly depend on that. The kind of solution I offered is
easily usable with for instance forms on the web, where one'd some
properties of an object as checkboxes, which the user could check &
uncheck (thus resulting in formentioned array and requirements). That
would for UI/clarities sake have to be limited to no more then 20 or 30
choices. In that case this'd be perfectly suitable, especially with
the UNIQUE key on the column.

When the data is indeed over 100's of rows or so I'd definitely look into
the temporary table solution.
--
Rik Wasmus


From: kath Date:   Friday, October 26, 2007
Load the array into a temporary table and use a MySQL intersect


I wouldn't consider this is better idea because, there creating table
inserting values will take lot of SQL queries(statements).
- CREATE statement = 1
- INSERT statement = depending on the size of array
- DELETE statement = 1

0: set up table:
Give the field a UNIQUE index
1: delete:
DELETE FROM tablename WHERE fieldname NOT IN ('list','of','values');
2: insert:
INSERT IGNORE INTO tablename (fieldname) VALUES ('list'),('of'),('values');
(allthough INSERT ... ON DUPLICATE KEY UPDATE ... could be more suited to
your needs)



Looks good. But I have doubt again, I am using JAVA to do this task. I
do not know exactly whether I can use String[] (array of String in
JAVA) in the DELETE query you've mentioned. I can construct a String
out list of String so that I can query, is it right way?.

If I get how to form a query for list of values, for above DELETE then
I as well get idea to INSERT query.

How do I form a query for list of String in JAVA?.

Thanks for your input,
best regards,
kath.


From: Rik Wasmus Date:   Friday, October 26, 2007
wrote in message:=> Load the array into a temporary table and use a MySQL intersect
I wouldn't consider this is better idea because, there creating table=

inserting values will take lot of SQL queries(statements).
- CREATE statement =3D 1
- INSERT statement =3D depending on the size of array



No, 1 INSERT statement'd do it.

- DELETE statement =3D 1



A lot of SQL queries (actually this is quite modest amount) does not =

necessarily mean it takes longer.

0: set up table:
Give the field a UNIQUE index
1: delete:
DELETE FROM tablename WHERE fieldname NOT IN ('list','of','values');=

2: insert:
INSERT IGNORE INTO tablename (fieldname) VALUES =

('list'),('of'),('values');
(allthough INSERT ... ON DUPLICATE KEY UPDATE ... could be more suit=
ed =



to
your needs)
Looks good. But I have doubt again, I am using JAVA to do this task. I=

do not know exactly whether I can use String[] (array of String in
JAVA) in the DELETE query you've mentioned. I can construct a String=

out list of String so that I can query, is it right way?.
If I get how to form a query for list of values, for above DELETE then=

I as well get idea to INSERT query.
How do I form a query for list of String in JAVA?.



Personally I stay as far away from JAVA as I can, put I assume an array =
in =

JAVA can be imploded/joined somehow to a string, which'd make creati=
ng =

the query string quite easy.
-- =

Rik Wasmus



Next Message: Encryption with UTF8 charset issue


Blogs related to how to compare array of String with column of a table

postgraduate universities in sweden
... mysql postgres mysql benchmark postgres mysql benchmarks postgres mysql comparison postgres mysql convert postgres mysql performance postgres null postgres nvl postgres odbc postgres odbc connection postgres odbc connection string ...

array c++ defining in string
array c++ class array c++ code array c++ defining in string array c++ dimensional two array c++ dynamic array c++ every other array c++ example array c++ file in into read vector array c++ file input parallel array c++ in array c++ ...

sorteo tec.com
... a listbox sorting a listview sorting a multidimensional array sorting a multidimensional array php sorting a set sorting a set in java sorting a set java sorting a spreadsheet sorting a string sorting a string array sorting a string ...

mysql my conf
mysql my cnf bind address mysql my conf mysql myodbc mysql myodbc not found mysql natural sort mysql navicat mysql ndb mysql ndb cluster mysql near right server syntax use version mysql nested mysql nested queries mysql nested query ...

php mysql client api version
... mysql column php mysql column name php mysql column names php mysql command php mysql command line php mysql commands php mysql commerce web hosting php mysql configuration php mysql configure php mysql connection string php mysql ...

asp cogged pulley
... asp company hosting professional asp company hosting web asp company hosting window asp comparable id inurl asp compare dates asp compare ecommerce solution asp compare string asp compare strings asp comparing dates asp comparison ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional