Javscript - 如何在不使用自动增量的情况下增加sql表中的数字 [英] Javscript-How to increment a number in an sql table without using autoincrement

查看:103
本文介绍了Javscript - 如何在不使用自动增量的情况下增加sql表中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:cust_code = 000001,这应该是我的customer表中的起始cust_code,并且应该像000002,000003..etc那样相应地增加...将新字段插入到此表中,这意味着按表应该看起来像, />
cust_code cust_name

000001 abc

000002 dca





。然后继续。

For example: cust_code=000001,this should be the starting cust_code in my customer table and should increment accordingly like 000002,000003..etc.. on inserting new fields into this table that means by table should look like,
cust_code cust_name
000001 abc
000002 dca
.
.
.and it goes on.

function pad(number, length)
{
   alert(number);
    var str = '' + number;
    while (str.length < length)
     {
        str = '0' + str;
    }

      return '000'+str;

}







var s="000001";

var number=parseInt(s.substring(3,6),10)+1;
         var value=pad(number,3);

		      transaction.executeSql('INSERT INTO customer(cust_code,cust_name)VALUES(?,?)',[value,$('#name').val()],populate_success,transaction_error );





我试过这段代码,但每次都是我插入,cust_code将与000002相同,因为我给出的初始值是000001.

任何人都可以帮我解决这个问题......



I have tried this code but everytime I insert, the cust_code will be same as 000002,because the initial value I have given is 000001.
Can anybody help me to solve this...

推荐答案

' #name')。val()],populate_success,transaction_error) ;
('#name').val()],populate_success,transaction_error );





我尝试过这段代码,但每次插入时,cust_code都与000002相同,因为我给出的初始值是000001.

任何人都可以帮我解决这个问题......



I have tried this code but everytime I insert, the cust_code will be same as 000002,because the initial value I have given is 000001.
Can anybody help me to solve this...


不要。甚至不要考虑它。



停下片刻,想想你想要做什么:这是一个网站,意味着多个同步更新。在网上,这是缓慢的,非确定性的。通过Web服务器(或者更可能是Web服务器场,这意味着每次用户与之通信时,您可能无法获得相同的物理服务器)。这将与运行SQL的不同计算机(或很可能是SQL服务器场中的许多此类服务器之一)进行通信,因此,您可能不会连续两次获得同一台计算机。



你将要让浏览器说:给我使用最多的号码,增加它,然后将它作为它的号码保存回数据库?



你真的意识到灾难和不可预测的错误的秘诀是什么,不是吗?



Auto增量字段有一个原因:使用它们。以免SQL解决问题并返回给你刚才分配的数字。
Don't. Don't even think about it.

Stop for a moment and think about what you are trying to do: This is a website, which implies multiple simultaneous updates. Over the net, which is slow, and non-deterministic. via a webserver (or more likely a web server farm, which means you probably won't get the same physical server each time the user communicates with it). Which will talk to a different computer running SQL (or quite likely, one of a number of such servers in an SQL server farm, so again, you probably won't get the same computer twice in a row).

And you are going to have the browser say: "Give me the highest number in use", increment it, and then save it back to the database as "it's" number?

You do realize what a recipe for disaster and unpredictable bugs that is, don't you?

Auto increment fields are there for a reason: use them. Lest SQL sort out teh problems and return to you the number it just assigned.


首先你的s是本地的,每次尝试更新时都会启动。将其声明为全局并仅初始化一次并继续更新。对于填充,请按照链接 [ ^ ]
First of all your s is local and being initiated every time you try to update. Declare it as global and initialize it just once and continue updating. And for padding follow the link[^]


这篇关于Javscript - 如何在不使用自动增量的情况下增加sql表中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆