sql query find match

4 views (last 30 days)
mark
mark on 31 Jan 2015
Answered: Geoff Hayes on 1 Feb 2015
hello guys, can you help me? how can i prevent adding same information in the database using sql query?
this is my code
cname= get(c_name,'string');
conn = database('mydatabase_2','','')
curs = exec(conn,'select * from db1');
curs = fetch(curs);
curs.Data
sqlquery = ['select * from db1 '...
'where cname = ' cname ];

Accepted Answer

Geoff Hayes
Geoff Hayes on 1 Feb 2015
Mark - I suspect that your query could be more like
sqlquery = ['select count(*) from db1 '...
'where cname = ''' cname '''' ];
Note that since the name is a string, you should wrap it in quotes. For example, if cname were Mark, then the above SQL query would become
sqlquery =
select count(*) from db1 where cname = 'Mark'
Note that we use count to determine the number of records in the database that match on the name Mark. You could then execute this query as
curs = exec(conn,sqlquery);
curs = fetch(curs);
curs.Data
where curs.Data would be an integer value that you would use to determine whether you should add the information to the database (if zero) or not (if non-zero).

More Answers (0)

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!