Tweepy information into MySQL - text being cut off - mysql

I am inserting information from twitter using Tweepy into a MySQL table. Mostly it works are intended however, the actual text from the tweet is being cut off and many end with "..." and I am not sure why this is.
An example of the output is this:
#JamesMelville: Remain voters are not "unpatriotic" by "talking the country down".Its the opposite. Remainers care about Britains fut…
Thanks for your help
Here is my stream listener class: (I am new to python so this is probably poor)
class listener(StreamListener):
def on_data(self, data):
try:
jsonData=json.loads(data)
tweets =(jsonData['text'])
if tweets.startswith('RT'):
retweets = True
else:
retweets = False
tweets = tweets.replace('RT', '')
tweets = tweets.replace("'", '')
tweets = tweets.lstrip()
tweets = tweets.replace('\n', '')
screen_name =(jsonData['user']['screen_name'])
name =(jsonData['user']['name'])
name = name.replace(',', ' ')
language =(jsonData['lang'])
location =(jsonData['place'])
coord =(jsonData['coordinates'])
device = jsonData['source'].split('">')[1].replace('</a>', '')
tweettime = jsonData['created_at'].replace('+', '')
tweettime = datetime.datetime.strptime(tweettime, "%a %b %d %H:%M:%S %f %Y")
date_to_string = str(tweettime.strftime("%Y/%m/%d,%H:%M:%S"))
date_created = date_to_string.split(',')[0]
time_created = date_to_string.split(',')[1]
created_time =(time_created)
created_date =(date_created)
htext = jsonData['entities']['hashtags']
htext2 = []
hashtag_list = ''
for hashtag in htext:
htext=str(hashtag['text'])
hashtag_list = hashtag_list + ' ::' + htext
hashtag_list = hashtag_list.replace("'", "")
conn = connect( host = '', port = , user = ', passwd = '', charset='utf8', autocommit = True);
conn.commit()
cursor = conn.cursor( cursors.DictCursor );
cursor.execute("CREATE DATABASE IF NOT EXISTS twitter")
cursor.execute("USE twitter")
cursor.execute( """CREATE TABLE IF NOT EXISTS `twitter_data`(ID INT AUTO_INCREMENT NOT NULL,`Name` VARCHAR( 200 ) ,`Screen name` VARCHAR( 200 ) , `Date created` VARCHAR ( 20 ), `Time created` VARCHAR ( 8 ), Tweet VARCHAR ( 200 ), Hashtags VARCHAR ( 200 ), Retweets VARCHAR ( 5 ), Lanugage VARCHAR ( 20 ), Device VARCHAR ( 60 ), Location VARCHAR( 200 ), Coordinates VARCHAR ( 200 ), PRIMARY KEY ( ID ))""" )
sql = "INSERT INTO `twitter_data` VALUES( Null, '" + str(name) + "', '" + str(screen_name) + "', '" + str(created_date) + "', '" + str(created_time) + "', '" + str(tweets) + "', '" + str(hashtag_list) + "', '" + str(retweets) + "', '" + str(language) + "', '" + str(device) + "', '" + str(location) + "', '" + str(coord) + "') "
print(sql)
cursor.execute(sql)
return True
except Exception as N:
print('failed on_data '+ str(N))
time.sleep(5)

I have found a solution to my question.
The text was being truncated when it was a retweet due to the 140 character limit.
Therefore it from nothing to do with MySQL.
My solution was to use the 'retweet_status' attribute (and split out the actual tweet text part) rather than 'text' if the tweet was a retweet, otherwise I just used 'text'.
Here is some of the code that was changed:
if tweets.startswith('RT'):
retweets = True
tweets = str(jsonData['retweeted_status']).split('\'text\':')[1]
if 'display_text_range' in tweets:
tweets = tweets.split(', \'display_text_range\'')[0]
else:
tweets = tweets.split(', \'source\'')[0]
else:
retweets = False
tweets = tweets.replace('RT', '')

Related

Node js - Mysql query is not executed as coded

i created several sql statements in node.js and now i want to execute them on my db. However, the query string is not executed as coded.
This is my function to generate the query string.
function insertProducts(products) {
if (!connection) {
// Create MYSQL-Connection
console.log('BUILDING connection to DB');
connection = getConnection();
connection.connect();
}
let query = "";
for (let i = 0; i < products.length; i++) {
// Iterate trough the products array and create a sql query
query += "INSERT INTO `tShortDescription`(`ShortDescription`, `Language`) VALUES ('" + products[i].short_description + "', 'DE'); " +
"INSERT INTO `tDescription`(`Description`, `Language`) VALUES ('" + products[i].description + "', 'DE'); " +
"INSERT INTO `tManufacturer`(`Name`) VALUES ('" + products[i].manufactur + "'); " +
"INSERT INTO `tSupplier`(`Name`) VALUES ('" + products[i].supplier + "'); " +
"INSERT INTO `tProduct`(`Sku`, `Title`, `ShortDescriptionId`, `DescriptionId`, `WohlesalePrice`, `SellingPrice`, `Quantity`, " +
"`ManufacturerId`, `SupplierId`, `Ean`) VALUES ('" + products[i].sku + "', '" + products[i].name + "', " +
"(SELECT id FROM tShortDescription WHERE ShortDescription = '" + products[i].short_description + "' LIMIT 1), " +
"(SELECT id FROM tDescription WHERE Description LIKE '" + products[i].description + "' LIMIT 1), " +
products[i].wholesale_price + ", " + products[i].selling_price + ", " + products[i].quantity + ", " +
"(SELECT id FROM tManufacturer WHERE Name = '" + products[i].manufactur + "' LIMIT 1), " +
"(SELECT id FROM tSupplier WHERE Name = '" + products[i].supplier + "' LIMIT 1), " + products[i].ean + "); ";
for (let j = 0; j < products[i].categories.length; j++) {
// Ad all categories to query
query += "INSERT INTO `rtCategory`(`ProductId`, `CategoryId`) " +
"VALUES ((SELECT `Id` FROM `tProduct` WHERE sku = '" + products[i].sku + "' LIMIT 1), " +
"(SELECT `Id` FROM `tCategory` WHERE Id = " + products[i].categories[j].src + " LIMIT 1)); "
for (let c = 0; c < products[i].images.length; c++) {
// Ad all images to query
query += "INSERT INTO `tImage`(`Url`) VALUES ('" + products[i].images[c].src + "'); " +
"INSERT INTO `rtImage`(`ProductId`, `ImageId`) " +
"VALUES ((SELECT `Id` FROM `tProduct` WHERE sku = '" + products[i].sku + "' LIMIT 1), " +
"(SELECT `Id` FROM `tImage` WHERE url = '" + products[i].images[c].src + "' LIMIT 1)); "
}
}
}
query = query.replace(/[\n\r\t]/g,);
if (query != "") {
// Create new Product in DB
return new Promise((resolve, reject) => {
connection.query(query, function (error, results, fields) {
if (error) { console.log(error) };
console.log('INSERTING successful');
resolve(results);
});
});
} else {
console.log('There are no new products to insert in db');
}
}
If i console.log(query) (before the query is ecexuted on my db) and execute the string directly in php myadmin, everything works fine but if i execute the query in code like connection.query(query, function (error, results, fields)....., i got several errors.
Error msg in terminal:
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO `tDescription`(`Description`, `Language`) VALUES ('<p><strong>Tantra' at line 1",
sqlState: '42000',
index: 0,
I also get the sql query returned in terminal because of the error, and if i execute this query directly in php myadmin i also get an error ->
SQL query: Documentation
INSERT INTO `rtImage`(`ProductId`, `ImageId`) VALUES ((SELECT `Id` FROM `tProduct` WHERE sku = 'H1500148' LM
IT 1), (SELECT `Id` FROM `tImage` WHERE url = 'https://cdnbigbuy.com/images/H1500148_409897.jpg' LIMIT 1))
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LM
IT 1), (SELECT `Id` FROM `tImage` WHERE url = 'https://cdnbigbuy.com/images' at line 1
It looks as if the LIMIT is somehow divided ...use near 'LM IT 1)....
I hope you understand where the problem is and someone might have a tip.
Your query is processed as 'LIMIT' it's just a new line in the console where the error showed up.
You should not be using string concatenation (or even template literals) for SQL queries under any circumstances because 1. It very likely the source of your problem. 2. It's very dangerous as it allows SQL injection attacks.
Use parameters instead. Here's a example:
connection.query("SELECT * FROM bank_accounts WHERE dob = ? AND bank_account = ?",[
req.body.dob,
req.body.account_number
],function(error, results){});
To read more about SQL injections and placeholders read this article.
Thanks for the helpful tips.
The problem was that I didn't set multiple statements: true in my code. This var is by default false and should be true, otherwise it is not possible to execute several queries once at a request!

How to store each sql result in a dataframe generated in loops?

I am trying to measure the query processing times of each SQL query. I need to run some SQL queries more than once, but with a randomly generated date range. So i need to save all the results generated in the loop of queries, but in different data frames.
I have tried using globals(), but the problem is i can't generate the shape of the results saved in those lists.
import MySQLdb
import random
from random import randint
import datetime
from datetime import timedelta
import time
import numpy as np
import pandas as pd
db_connection = MySQLdb.connect(host="localhost", user="root", passwd="050194.Piku", db = "lineitem")
cursor = db_connection.cursor()
for x in range(2):
date_range1 = datetime.date(randint(1992, 1995), randint(1, 12), randint(1, 30))
date_range2 = datetime.date(randint(1996, 1998), randint(1, 12), randint(1, 30))
mdate1 = str(date_range1.year) + "-" + str(date_range1.month) + "-" + str(date_range1.day)
mdate2 = str(date_range2.year) + "-" + str(date_range2.month) + "-" + str(date_range2.day)
orderkey = str(randint(1, 6000000))
lineitem_extended_price_range1 = round(random.uniform(900, 90000), 5)
lineitem_extended_price_range2 = round(random.uniform(90001, 110000), 5)
lineitem_ext_price1 = str(lineitem_extended_price_range1)
lineitem_ext_price2 = str(lineitem_extended_price_range2)
order_total_price_range1 = round(random.uniform(850, 85000), 5)
order_total_price_range2 = round(random.uniform(85001, 560000), 5)
order_total_price1 = str(order_total_price_range1)
order_total_price2 = str(order_total_price_range2)
sql_query_lineitem1 = "SELECT * FROM lineitem_table WHERE L_SHIPDATE BETWEEN '" + mdate1 + "' AND '" + mdate2 + "' LIMIT 10;"
# sql_query_lineitem2 = "SELECT * FROM lineitem_table WHERE L_EXTENDEDPRICE BETWEEN '" + lineitem_ext_price1 + "' AND '" + lineitem_ext_price2 + "';"
# sql_query_lineitem3 = "SELECT * FROM lineitem_table WHERE L_ORDERKEY = '" + orderkey + "';"
# sql_query_order4 = "SELECT * FROM order_table WHERE O_ORDERKEY = '" + orderkey + "';"
# sql_query_order5 = "SELECT * FROM order_table WHERE O_ORDERDATE BETWEEN '" + mdate1 + "' AND '" + mdate2 + "';"
# sql_query_order6 = "SELECT * FROM order_table WHERE O_TOTALPRICE BETWEEN '" + order_total_price1 + "' AND '" + order_total_price2 + "';"
# sql_query_join = "SELECT * FROM lineitem_table INNER JOIN order_table ON lineitem_table.L_ORDERKEY = order_table.O_ORDERKEY;"
globals()["mdate1" + str(x)] = mdate1
globals()["mdate2" + str(x)] = mdate2
globals()["ext_price1" + str(x)] = lineitem_ext_price1
globals()["ext_price2" + str(x)] = lineitem_ext_price2
globals()["orderkey" + str(x)] = orderkey
globals()["total_price1" + str(x)] = order_total_price1
globals()["total_price2" + str(x)] = order_total_price2
#average_execution_sum = 0
#initial_time1 = time.time()
cursor.execute(sql_query_lineitem1)
d = pd.DataFrame.from_records(cursor.fetchall(), columns=[desc[0] for desc in cursor.description])
#time_taken1 = time.time() - initial_time
# cursor.execute(sql_query_lineitem2)
# globals()["df_02" + str(x)] = pd.DataFrame.from_records(cursor.fetchall(), columns=[desc[0] for desc in cursor.description])
#
#
# cursor.execute(sql_query_lineitem3)
# globals()["df_03" + str(x)] = pd.DataFrame.from_records(cursor.fetchall(), columns=[desc[0] for desc in cursor.description])
#
#
# cursor.execute(sql_query_order4)
# globals()["df_04" + str(x)] = pd.DataFrame.from_records(cursor.fetchall(), columns=[desc[0] for desc in cursor.description])
#
#
# cursor.execute(sql_query_order5)
# globals()["df_05" + str(x)] = pd.DataFrame.from_records(cursor.fetchall(), columns=[desc[0] for desc in cursor.description])
#
#
# cursor.execute(sql_query_order6)
# globals()["df_06" + str(x)] = pd.DataFrame.from_records(cursor.fetchall(), columns=[desc[0] for desc in cursor.description])
#
#
# cursor.execute(sql_query_join)
# globals()["df_03" + str(x)] = pd.DataFrame.from_records(cursor.fetchall(), columns=[desc[0] for desc in cursor.description])
#
cursor.close()
db_connection.close()
print(df_010.shape(0))
TypeError: 'tuple' object is not callable
Maybe you can try to use
df = pd.read_sql_query("SELECT * FROM table_name", db_connection)
to store the selected table from your SQL query to a pandas data frame df.

How to solve? ER_BAD_FIELD_ERROR: Unknown column 'undefined' in 'field list'

I am trying to insert form data into MySQL database in nodejs using expressjs
When I run my code in command prompt it ran well but when I press the submit button, I got the following errors:
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password:'',
database : 'test'
});
app.get("/", function(req, res){
res.render("home");
});
//when I press submit button it should post the request and render a page to submit route with text "data saved!!"
app.post("/submit", function(req, res){
var q = "Insert into test (ID, name, crash1, crash2, crash3) VALUES (null, '" + req.body.ANR + "', " + req.body.crash1 + ", " + req.body.crash2 + ", " + req.body.crash3 +")";
connection.query(q, function(err){
if(err) throw err
res.render("home", {message: 'data saved!!'});
})
});
I created a table in MySQL Command line
create table xyz(
ID BIGINT AUTO_INCREMENT PRIMARY KEY NOT NULL,
name VARCHAR(100) NOT NULL,
crash1 BIGINT,
crash2 BIGINT,
crash3 BIGINT
);
when I inserted manually it worked!
insert into xyz(ID, name, crash1, crash2, crash3) VALUES (1,'REERE', 2 ,2 ,2);
my error looks like this
You are inserting into test table in your code:
var q = "Insert into test (ID, name, crash1, crash2, crash3) VALUES (null, '" + req.body.ANR + "', " + req.body.crash1 + ", " + req.body.crash2 + ", " + req.body.crash3 +")";
But table name is xyz. You should replace test by xyz and it should work.
And don't pass null in id as well as id is not null.
Please convert crash1, crash2, crash3 into int value:
req.body.crash1 = parseInt(req.body.crash1);
req.body.crash2 = parseInt(req.body.crash2);
req.body.crash3 = parseInt(req.body.crash3);
It should be like:
var q = "Insert into xyz (name, crash1, crash2, crash3) VALUES ('" + req.body.ANR + "', " + req.body.crash1 + ", " + req.body.crash2 + ", " + req.body.crash3 +")";

Error in updating database row in MYSQL

I have a table
items: id, userid, item_name, item_description
I want to update a row and used the following sql statement for it.
$updateQuery = "UPDATE items SET item_name = '$item_name',
item_desc = '$item_desc' WHERE userid = '$userid'
AND item_name = '$old_name'";
But it fails. Is it because I used the item_name field, which is to be updated, for selecting the row?
I think I see the problem
item_desc = '$item_desc'
"4 columns id, userid, item_name, item_description."
Change your query to
$updateQuery = "UPDATE items SET item_name = '$item_name', item_description = '$item_desc' WHERE userid = '$userid' AND item_name = '$old_name'";
you not update item_name because you used it in where clause
or
you can echo this string and run in database terminal to verify.
Try :
$updateQuery = "UPDATE items SET item_name = '" . $item_name . "', item_desc = '" . $item_desc . "' WHERE userid = " . $userid . " AND item_name = '" . $old_name . "';"
Please notice, in your query, you are referring the last column as "item_desc" which does not exist, as the actual column name is "item_description" .
MySQL is treating "item_desc" as a separate column in your table, but unable to find it, and hence the error.
Also, it is a good idea to pay attention to how you are concatenating your variable to your query. After equal to(=) sign, always use this notation ' ".$variable_name." ' to concatenate. Example:
select column1, column2 from table1 where (column1 = ' ".$variable_name." ' && column2 = ' ".$variable_name." ') ";
You have to concatenate the strings.
$updateQuery = "UPDATE items SET item_name = '" . $item_name . "', item_desc = '" . $item_desc . "' WHERE userid = " . $userid . " AND item_name = '" . $old_name . "'";
Instead of item_desc, it should be item_description.

register data from erlang to table mysql

I have a table in mysql named person
this a simple code of insertion of data in the table person
$id = "1";
$firstname = "afif";
$lastname = "kaled";
$test = mysql_connect("localhost", "root", "root");
if ($test) {
mysql_select_db("basetest", $test);
}
$sql = " INSERT INTO `person` SET
`id` = '" . $id . "',
`firstname` = '" . $firstname . "',
`lastname` = '" . $lastname . "' ";
#mysql_query($sql, $test);
I want to modify this function
test()->
Id ="11",
Firstname ="afif",
Lastname ="kaled",
%% here I want to register this data in the table person .
so the table person will have this data
11 afif kaled
I want to know if is it possible to register data from erlang to table mysql
I have already done an example of transfer data from erlang to txt file with this code :
exporttxt()->
F = fun() -> mnesia:foldl(fun(X,Acc) -> [X|Acc] end, [],person) end,
{atomic,L} = mnesia:transaction(F),
file:write_file("test.txt",[io_lib:format("~p\t~p\t~p~n",[F1,F2,F3]) ||
#person{id = F1,firstname = F2,lastname = F3} <- L]).
but now as I already said I want to know is it possible or not to send data from erlang to a table in mysql
Of course it is possible.
Try to use a search before asking questions.