PREG REPLACE ERROR ALPHA NUMERIC STRING - MYSQL - mysql

Hi I used the preg_replace for replace the non alpha numeric data with ''.
1)
SELECT
PREG_REPLACE('[^A-Za-z0-9]','',CATALOGUEREFERENCE) AS INSTRUMENT
FROM
FILES;
2)
select
preg_replace('[[:alnum:]]', '',CATALOGUEREFERENCE) as instrument
from
files;
First query displays the records as blob and the second query displays null records. Can anyone suggest the query modification for replacing the non alpha numeric data with ''.
Input file :
GETA2000003890
MAC00007000
NAS00006000
MAS000007000
MAS2SA200006000
Desired output:
GET
MAC
NAS
MAS
MAS2SA2
Thanks a lot

I think your desired output is: GET MAC NAS MAS MAS.
Code:
$input = 'GETA2000003890 MAC00007000 NAS00006000 MAS000007000 MAS2SA200006000';
$output = preg_replace('/([A-Z]{3})(\w*)/i', '$1', $input);
echo $output; // GET MAC NAS MAS MAS

Related

Pig: Create json file with actual key_name and values

I have a pig script using elephant bird json loader.
data_input = LOAD '$DATA_INPUT' USING com.twitter.elephantbird.pig.load.JsonLoader() AS (json:map []);
x = FOREACH data_input GENERATE json#'user__id_str', json#'user__created_at', json#'user__notifications', json#'user__follow_request_sent', json#'user__friends_count', json#'user__name', json#'user__time_zone', json#'user__profile_background_color', json#'user__is_translation_enabled', json#'user__profile_link_color', json#'user__utc_offset', json#'user__profile_sidebar_border_color', json#'user__has_extended_profile', json#'user__profile_background_tile', json#'user__is_translator', json#'user__profile_text_color', json#'user__location', json#'user__profile_banner_url', json#'user__profile_use_background_image', json#'user__default_profile_image', json#'user__description', json#'user__profile_background_image_url_https', json#'user__profile_sidebar_fill_color', json#'user__followers_count', json#'user__profile_image_url', json#'user__geo_enabled', json#'user__entities__description__urls', json#'user__screen_name', json#'user__favourites_count', json#'user__url', json#'user__statuses_count', json#'user__default_profile', json#'user__lang', json#'user__protected', json#'user__listed_count', json#'user__profile_image_url_https', json#'user__contributors_enabled', json#'user__following', json#'user__verified';
STORE x INTO '$DATA_OUTPUT' USING JsonStorage();
I have the output right but the field names are wrong.
My output has val_n instaed of the field names themselves:
{"val_0":"40510796","val_1":"Sat May 16 18:03:53 +0000 2009","val_2":"false"......}
I want something like:
{"user__id_str":"40510796","user__created_at":"Sat May 16 18:03:53 +0000 2009","user__notifications":"false"...........}
How can I get the column names as well?
Have you tried giving Alias in generate statement:
x = FOREACH data_input GENERATE json#'user__id_str' AS user__id_str, json#'user__created_at' AS user__created_at;

sqldf query returns 0 values

I am having an issue running a basic query on a sample dataset(link below)
http://kbcdn.tableausoftware.com/data/Superstore.xls
using R.
I have attached my code below.
#read file with XLConnect
path <- file.path("/Users/petergensler/Desktop/Sample - Superstore Sales.xls")
superstore <- readWorksheetFromFile(path, sheet= "Orders")
#Query
test <- sqldf("SELECT * FROM superstore WHERE 'Product Sub-Category' = 'Appliances'",)
test
The query executes fine, but it returns the following results:
[1] Row.ID Order.ID Order.Date Order.Priority Order.Quantity Sales
[7] Discount Ship.Mode Profit Unit.Price Shipping.Cost Customer.Name
[13] Province Region Customer.Segment Product.Category Product.Sub.Category Product.Name
[19] Product.Container Product.Base.Margin Ship.Date
<0 rows> (or 0-length row.names)
Is there something wrong with my attached packages that would be causing the query to run wrong, or is it something with my data? the column I am querying on seems to be fine, as it is a type character, and specifying a literal string should match the values(unless their is trailing whitespace), correct?
I am running R on Mac OS X 10.11.5 with the following session info:
session_info()
Session info -------------------------------------------------------------------------------------------
setting value
version R version 3.3.0 (2016-05-03)
system x86_64, darwin13.4.0
ui RStudio (0.99.896)
language (EN)
collate en_US.UTF-8
tz America/Chicago
date 2016-06-08
I have also attached my packages attached to the current session as well.
https://drive.google.com/open?id=0Bxhxg_yftHNubEc4NUZTUVoxa0E
Thanks for your help!
Following up on what G. Grothndieck said (use brackets for column names) I ran this and it worked for me:
#Query
test <- sqldf(x = "SELECT * FROM superstore WHERE [Product.Sub.Category] = 'Appliances'")
test
Most methods of reading in Data frames change spaces and hyphens in column names into . so you need to update that part of it.

Mysql showing type error in python

I wrote the following code to write into mysql database. It shows "%d format: a number is required, not str"
whereas when I print number, it shows 6. why so ??
for i in range(len(text1)):
try1="INSERT INTO `crawl_url_attribute_css_sel_value`(`crawl_item_id`,`crawl_url_attribute_css_sel_id`,`value`,`reconciled`)\
VALUES(%s,%d,%s,FALSE) "
values=(prodlist[i].get_attribute(result1[4]), int(number), db.escape_string(text1[i].text))
print number
cursor.execute(try1,values)
db.commit()
You have to use %s placeholder.
for i in range(len(text1)):
try1="INSERT INTO `crawl_url_attribute_css_sel_value`
(`crawl_item_id`, `crawl_url_attribute_css_sel_id`, `value`, `reconciled`)\
VALUES(%s,%s,%s,FALSE)"
values=(prodlist[i].get_attribute(result1[4]), int(number), db.escape_string(text1[i].text))
print number
cursor.execute(try1,values)
db.commit()
You can read the explanation here
To perform a query, you first need a cursor, and then you can execute
queries on it:
c=db.cursor() max_price=5 c.execute("""SELECT spam, eggs, sausage FROM
breakfast
WHERE price < %s""", (max_price,))
In this example, max_price=5 Why, then, use %s in the string? Because
MySQLdb will convert it to a SQL literal value, which is the string
'5'. When it's finished, the query will actually say, "...WHERE price
< 5".

A shorter non-repeating alphanumeric code than UUID in MySQL

Is it possible for MySQL database to generate a 5 or 6 digit code comprised of only numbers and letters when I insert a record? If so how?
Just like goo.gl, bit.ly and jsfiddle do it. For exaple:
http://bit.ly/3PKQcJ
http://jsfiddle.net/XzKvP
cZ6ahF, 3t5mM, xGNPN, xswUdS...
So UUID_SHORT() will not work because it returns a value like 23043966240817183
Requirements:
Must be unique (non-repeating)
Can be but not required to be based off of primary key integer value
Must scale (grow by one character when all possible combinations have been used)
Must look random. (item 1234 cannot be BCDE while item 1235 be BCDF)
Must be generated on insert.
Would greatly appreciate code examples.
Try this:
SELECT LEFT(UUID(), 6);
I recommend using Redis for this task, actually. It has all the features that make this task suitable for its use. Foremost, it is very good at searching a big list for a value.
We will create two lists, buffered_ids, and used_ids. A cronjob will run every 5 minutes (or whatever interval you like), which will check the length of buffered_ids and keep it above, say, 5000 in length. When you need to use an id, pop it from buffered_ids and add it to used_ids.
Redis has sets, which are unique items in a collection. Think of it as a hash where the keys are unique and all the values are "true".
Your cronjob, in bash:
log(){ local x=$1 n=2 l=-1;if [ "$2" != "" ];then n=$x;x=$2;fi;while((x));do let l+=1 x/=n;done;echo $l; }
scale=`redis-cli SCARD used_ids`
scale=`log 16 $scale`
scale=$[ scale + 6]
while [ `redis-cli SCARD buffered_ids` -lt 5000 ]; do
uuid=`cat /dev/urandom | tr -cd "[:alnum:]" | head -c ${1:-$scale}`
if [ `redis-cli SISMEMBER used_ids $uuid` == 1]; then
continue
fi
redis-cli SADD buffered_ids $uuid
done
To grab the next uid for use in your application (in pseudocode because you did not specify a language)
$uid = redis('SPOP buffered_ids');
redis('SADD used_ids ' . $uid);
edit actually there's a race condition there. To safely pop a value, add it to used_ids first, then remove it from buffered_ids.
$uid = redis('SRANDMEMBER buffered_ids');
redis('SADD used_ids ' . $uid);
redis('SREM buffered_ids ' . $uid);

mysql_query on utf8_general_ci collation

I'm having some trouble with running a query on a database using utf8 encoding.
Everything works fine until I try a string with special characters.
I can't really make a lot of changes to the database since I'm adding some separate pages to an existing prestashop template.
This is my code (sorry, but some of it is in Romanian)
$acces_server=mysql_connect($db_host, $db_user, $db_pass);
if(!$acces_server) die ("Server-ul nu este disponibil momentan !");
mysql_set_charset('utf8');
echo mysql_client_encoding();
$acces_bd = mysql_select_db($db_name);
if (!$acces_bd) die ("Ne pare rau, dar baza de date nu poate fi accesata momentan !");
mysql_query('SET NAMES utf8') or die(mysql_error());
mysql_query('SET CHARACTER SET utf8') or die(mysql_error());
mysql_query('SET COLLATION_CONNECTION="utf8_general_ci" ') or die(mysql_error());
If I try to make a query like this I get 0 rows:
$experienta= $_GET['name'];
$rez=mysql_query("SELECT nume_cutie FROM rez_experiente WHERE nume = '".$experienta."'");
I've used echo to display the query and then typed it in phpMyAdmin and it worked .
Also the head section has this line :
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
I really don't know what else to do , all the solutions I've found so far don't seem to work. I have even tried using mysqli instead and the result was the same.
Please help me , and thank you for your time.
*Edit
I tried using stmt->prepare with mysqli and that didn't work.
I've also tried doing this :
$experienta2=str_replace("?","%",utf8_decode($experienta));
$rez=mysql_query("SELECT nume_cutie FROM rez_experiente WHERE nume LIKE '".$experienta2."'");
Wich turns this :
SELECT nume_cutie FROM rez_experiente WHERE nume LIKE '5th Avenue Beauty Center - Răsfăţ'
Into this:
SELECT nume_cutie FROM rez_experiente WHERE nume LIKE '5th Avenue Beauty Center - R%sf%%'
And again the query works in phpMyAdmin but mysql_query() returns nothing
I switched to utf8_unicode_ci and nothing changed.
After the database connection, run these queries:
SET character_set_client=utf8;
SET character_set_connection=utf8;
SET character_set_database=utf8;
SET character_set_results=utf8;
SET character_set_server=utf8;