1

I have an app that I am building and I have to save settings for some of the pages. I've decided to save it in mysql in binary data format. I got a lot of checkboxes. So let's say i have this kind of form

<form>
<input type="text" value="some text" id="text">
<input type="checkbox" name="checkboxes1">
<input type="checkbox" name="checkboxes2">
<input type="checkbox" name="checkboxes3">
</form>

I have 10 pages that have settings page similar to this one. I think that I can write this data into mysql database in the form of binary data. Lets say the first one and the lest one are selected so I write in mysql 101. What kind of data type to choose BLOB, VARCHAR or something else?

4

2 回答 2

3

The most efficient way to store bit-wise data is to use integer data types. In your case, each check box would represent 1 bit of an integer. For example, if the check boxes were set:

checkbox 1 checked
checkbox 2 unchecked
checkbox 3 checked

this would represent 5 (1*2^2 + 0*2^1 + 1*2^0). Use the following types of integers depending on the number of bits that you need to store:

tiny int:    8  bits
small int:   16 bits
medium int:  24 bits
int:         32 bits
big int:     64 bits

See http://dev.mysql.com/doc/refman/5.6/en/integer-types.html for more info

于 2013-07-23T13:37:44.283 回答
0

You can create a table of default settings, then a corresponding table that will store individual user settings. You can use a bit or tinyint data type to indicate whether an option is turned off/on.

settingDefault
settingId  setting    default
1          loadImages 1
2          loadAds    0

settingUser
userId  settingId  value
1       1          0

The advantage here is that you only have to store user settings if they're different from the defaults.

于 2013-07-23T13:36:18.363 回答