Categories
MySQL sql

INSERT IGNORE MySQL

The INSERT MySQL statement allows you to add multiple rows into a table but should an error occur, MySQL terminates and returns an error. This means no row is inserted into the table. When you need to ignore the error and allow insertion of valid data the INSERT IGNORE statement is one you can use. MySQL will throw a warning but the valid data will be inserted into the table.

We will create a table members with a unique mobile column, this will be our constraint ensuring we don’t duplicate mobile numbers.

CREATE TABLE members (INT PRIMARY KEY AUTO_INCREMENT, mobile VARCHAR(13) NOT NULL UNIQUE
);

Now we insert a new row:

INSERT INTO members(mobile) VALUES('+254711111111');

This works fine, now lets try inserting three rows into the table

INSERT INTO members(mobile) VALUES('+254711111123'), ('+254711111112'), ('+254711111111');

Categories
Social Media

04.04.18

Designing this 04.04.18 poster for Pwani Innovation Week media brief was fun.

Created this media and asked the team to put as the WhatsApp status, profile pictures and all social media without any caption or any explanation. I giggled hearing people ask what’s happening on the date.

More designs:

I copied this from Jay-Z’s launch of the 4:44 album.

Image by thefader.com
Timothy A. Clary / AFP/Getty Images
Photo by Don Makameleon
Categories
sql

Generate random records for quick SQL learning

Here is a scenario; you want to run SQL queries on a large dataset but you do not this size of data in a database for you to be able to run your queries, the reason for wanting to do could be purely for learning something new or test on fake data. Luckily database systems have some form of random function to let us do this. The following SQL queries will help illustrate this:

First I will create a database called learnings and connect to this database

root=# CREATE DATABASE learnings; 
root=# \c learnings;

I will create the item table with name and price columns

Categories
scripting sql

What will be the date x days from now?

Today I learn some nifty shell command. The command simply answers the question, what will be date x days from now. So lets says I wanted to know what will be date and time 13 days from now

echo "select now() + '30 days'::interval" | psql

As you can see from this I am piping into psql (postgresql terminal frontend) so this is simply a SQL statement run from the shell.

Categories
Outside

Falconry of Kenya in Malindi

Categories
Django

Fix: ValueError: unknown locale: UTF-8

I was setting up the python.org website code, built on Django, on my local machine I encountered the error ValueError: unknown locale: UTF-8 while running the ./manage.py migrate command. See screenshot below:

Screenshot 2020-05-06 at 01.06.57

To fix this, I had to set the following environment variables:

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

There, back to code.

Categories
education

Education and Covid-19

I was watching a news clip on how some students and pupils cannot access any education material during this period of school shutdowns because of the covid-19 pandemic. These students do not have access to smartphones or televisions in their homes. The clip included a young girl pleading with the government to provide a smartphone to her parent so that she could continue learning.

There are a number of measures that the government has taken, these include working with education agencies to create content that is aired on television channels like EduTV and radio stations. A number of my friends (techies) mocked the plan to air content via radio, what they don’t realize is how many people have access to radio compared to other form of mass communication.

Looking at Volume 4 of the Kenya Housing and Population Census 2019, 56.9% of the population have access to radio while 8.8% has access to computer/laptop/tablet and only 17.9% have internet access. Report can be found here https://www.knbs.or.ke/?wpdmpro=2019-kenya-population-and-housing-census-volume-iv-distribution-of-population-by-socio-economic-characteristics

89904620_10158105226773664_2095504519264731136_n

Categories
mobile

Experience using Cellulant’s Tingg App

NOTE: Cellulant has not paid me to write this post

Cellulant launched Tingg mobile application in November 2019, I attended the launch of the mobile app when the team did a launch session at Swahilipot Hub. The app was initially called Mula during the Beta period of the launch.

The app allows a user to be able to pay for a number of services; PayTV, Buy Artime (across networks), Gas (order gas though it works in Nairobi as of writing of this post), Power (electricity tokens) and water services. This set of features are what will make some call Tingg a super app. Is it a super app already?

You can learn more about super apps here from a talk by Connie Chan of a16z (Andreessen Horowitz Venture Company)

Categories
Django

Migrating multiple CharFields to null=False

I am working on a simple project built on top of Django web framework. I have a user profile model that had 3 char fields that needed to be update from null=True to null=False

When I run python manage.py migrate on the django app I get the following error django.db.utils.OperationalError: cannot ALTER TABLE "accounts_profile" because it has pending trigger events To fix this I run the PostgreSQL shell (psql) to update and set the changed fields.

The SQL command for the fix is:
db=# update accounts_profile set non_nullable_field = '' where non_nullable_field is null;

I run the same command for the other two different fields.

Follow me on twitter @bmwasaru

Categories
git

Git pull multiple repos

I read a lot of open source projects, this has helped learn how to write code better. One of the interesting projects I follow is sentry (https://github.com/getsentry/sentry), sentry is cross-platform application monitoring, with a focus on error reporting.

The one challenge I have had is keeping up with the repositories, I needed a way to keep the repositories on my laptop up-to-date. Here is a one liner bash script I use to git pull the multiple repositories.

for i in */.git; do ( echo $i; cd $i/..; git pull; ); done