MySQL: Truncate Table within Transaction?

http://dev.mysql.com/doc/refman/5.1/en/truncate-table.html According to this URL, as of MySQL 5.1.32, TRUNCATE TABLE is DDL and NOT DML like DELETE. This means that TRUNCATE TABLE will cause an implicit COMMIT in the middle of a transaction block. So, use DELETE FROM on a table you need to empty instead of TRUNCATE TABLE. Even DELETE FROM tblname; can … Read more

Truncate a string without ending in the middle of a word

I actually wrote a solution for this on a recent project of mine. I’ve compressed the majority of it down to be a little smaller. def smart_truncate(content, length=100, suffix=’…’): if len(content) <= length: return content else: return ‘ ‘.join(content[:length+1].split(‘ ‘)[0:-1]) + suffix What happens is the if-statement checks if your content is already less than … Read more

How do I truncate a java string to fit in a given number of bytes, once UTF-8 encoded?

Here is a simple loop that counts how big the UTF-8 representation is going to be, and truncates when it is exceeded: public static String truncateWhenUTF8(String s, int maxBytes) { int b = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); // ranges from http://en.wikipedia.org/wiki/UTF-8 int skip = … Read more