Bitcoin Forum

Bitcoin => Project Development => Topic started by: KaozTiposta on June 25, 2015, 09:24:39 PM



Title: Best way to store bitcoin values in MySql?
Post by: KaozTiposta on June 25, 2015, 09:24:39 PM
Hello

I am developing a bitcoin application using Mysql.

What is the best way to store it? DECIMAL(16,8) or multiplying it with 100M?



Title: Re: Best way to store bitcoin values in MySql?
Post by: secrethedgehog on June 25, 2015, 09:39:05 PM
A few clients I have worked with store it as VARCHAR, I am no expert but I don't believe that VARCHAR is correct way to store a decimal number.

Personally I would store it as Satoshi BIGINT and then multiply using the language that pulls the data out.


Title: Re: Best way to store bitcoin values in MySql?
Post by: KaozTiposta on June 25, 2015, 09:41:30 PM
I'm so lazy to multiply/divide it everywhere.. Also VARCHAR sounds interesting to me.

So I think I will go with DECIMAL but this is a little sensitive project and I don't want to see round errors. Can DECIMAL has rounding errors ?


Title: Re: Best way to store bitcoin values in MySql?
Post by: secrethedgehog on June 25, 2015, 09:51:00 PM
What language are you using to pull the data?

I usually just create one function to multiply and format. Then I can just use that on whatever needs it.

I don't ever use Decimal in MySQL so I don't really know to be honest.

I was only using VARCHAR as a bad example. I wouldn't suggest this. It has caused me some problems in the past.

http://stackoverflow.com/questions/3008371/storing-numbers-as-varchar


Title: Re: Best way to store bitcoin values in MySql?
Post by: person on June 25, 2015, 09:56:41 PM
Store as satoshis: BIGINT
Manipulate as satoshis

Then you can convert in any type (BTC, mBTC) based on user preference only for display.


Title: Re: Best way to store bitcoin values in MySql?
Post by: aysha9859 on June 26, 2015, 09:53:24 AM
Store as satoshis: BIGINT
Manipulate as satoshis

Then you can convert in any type (BTC, mBTC) based on user preference only for display.
im using BIGINT on my faucet project. you can play however you want with satoshi. but sometimes im getting errors. for ex: i have 50000 in the cell. divide 100000000 i get output like 0.00049999 for solve this error im writing convert function. im still working on it.


Title: Re: Best way to store bitcoin values in MySql?
Post by: bitnanigans on June 26, 2015, 12:57:27 PM
You can use either DECIMAL(16,8) or BIGINT.
If you're storing with BIGINT, you will have to make sure that you convert your values correctly at all times, or you may run into several problems.
The decimal is used for precision math, so you won't encounter rounding errors with the data type.


Title: Re: Best way to store bitcoin values in MySql?
Post by: person on June 26, 2015, 02:35:56 PM
Store as satoshis: BIGINT
Manipulate as satoshis

Then you can convert in any type (BTC, mBTC) based on user preference only for display.
im using BIGINT on my faucet project. you can play however you want with satoshi. but sometimes im getting errors. for ex: i have 50000 in the cell. divide 100000000 i get output like 0.00049999 for solve this error im writing convert function. im still working on it.

For display, why divide if you can just add the . after 8 characters (as string)?


Title: Re: Best way to store bitcoin values in MySql?
Post by: wh00per on June 27, 2015, 05:59:55 PM
What's wrong with double? The precison is way better than what you need for satoshi transactions.

https://dev.mysql.com/doc/refman/5.0/en/fixed-point-types.html (https://dev.mysql.com/doc/refman/5.0/en/fixed-point-types.html)

You can also do BIGINT as satoshi, or varchar, however while bigint indexes nicely and it's quick, varchar is a little laggy.  

As of 5.0.6, MySQL performs DECIMAL (or NUMERIC) operations with a precision of 65 decimal digits. If you need exact values in your applications I'd rather use one of those two.


Title: Re: Best way to store bitcoin values in MySql?
Post by: Vortex20000 on June 28, 2015, 02:11:12 PM
Just use MySQL's double type, it's what I use and I've had any problems with it thus far.


Title: Re: Best way to store bitcoin values in MySql?
Post by: bitnanigans on June 28, 2015, 02:36:03 PM
Just use MySQL's double type, it's what I use and I've had any problems with it thus far.

This is a very wrong approach. See http://stackoverflow.com/questions/2251290/storing-money-amounts-in-mysql


Title: Re: Best way to store bitcoin values in MySql?
Post by: THE TRADER on June 28, 2015, 03:06:50 PM
Be Carefull ob un Mysql Injections. https://de.wikipedia.org/wiki/SQL-Injection

It`s maybe not actually the Securityst Technologie. Scrypt.cc got hacked by mysql injection. And used a got based on mysql/ddos page so far.

My opion but im not a Profi  ;D


Title: Re: Best way to store bitcoin values in MySql?
Post by: secrethedgehog on June 28, 2015, 04:26:21 PM
Be Carefull ob un Mysql Injections. https://de.wikipedia.org/wiki/SQL-Injection

It`s maybe not actually the Securityst Technologie. Scrypt.cc got hacked by mysql injection. And used a got based on mysql/ddos page so far.

My opion but im not a Profi  ;D

There are many techniques to stop this, You shouldn't be scared of using MySql, You should be scared of how good your coding is.... or not.

In PHP there are techniques like Prepared Statements and escaping characters.


Title: Re: Best way to store bitcoin values in MySql?
Post by: noel57 on June 29, 2015, 05:31:51 AM
Hello

I am developing a bitcoin application using Mysql.

What is the best way to store it? DECIMAL(16,8) or multiplying it with 100M?


Keep it simply simple, 8 digits still remain the best, any other extension will be boring and some-how confusing.