Using substr and replace in sqlite
I finally use substr(x,y,z)
to get the first z
chars from y
(I set 1 to get start at the beginning of the original string).
Then use ||
to connect two strings. It's a little different with the answer provided by @Hugh Bothwell.
After ||
, I use replace(substr(quote(zeroblob((Y + 1) / 2)), 3, Y), '0', '*')
to fill the string to length Y
. It's a little hard to understand at the beginning.This website has more information for you.
I need my string to be masked after 5th chars, so I don't need to append anything behind the replace
statement. If you need, just add another ||
and use substr()
.
In short, what I did is: +1-2024561111
(original string)
- substr() ➜
+1-202456
||
➜ to append another string
- the
replace
statement ➜ to fill string to a certain length ➜ +1-202456****
Another solution: in python
It's a normal statement to print database in sqlite3 in python like this:
for row in c.execute('SELECT * FROM stocks ORDER BY price'):
print row
I add a if-else
statement and use ljust to do what I want.
It's a sample below:
-- row[0]: the first filed in table, here means numbers
if len(row[0])>=9:
newNum=row[0][0:10].ljust(len(row[0]),'*')
#^^^^^^ to get first 9 chars in row[0]
# ljust fill in '*' from 10th char to the length of row[0]
print(newNum) # use newNum instead of row[0]