如何使用int并在不同的代码行中增加它的值

时间:2015-07-13 11:31:01

标签: java int

我必须初始化一个具有如下结构的sqlite数据库:

表:家庭

表:SubFamily

表:产品

一个家庭可以有一个以上的亚家族,而且对于有产品的亚家族也是如此。

所以我正在做的是:

int index_family = 1;
        int index_subfamily = 1;
        int index_product = 1;
//Productos Familia Pavimentos
        sqlDB.execSQL("INSERT INTO Family VALUES (" + index_family + ",'" + R.string.f1_pavimentos + "','" + R.string.f0_descripcion + "','" + R.drawable.color_pavimento + "');");

        sqlDB.execSQL("INSERT INTO SubFamily VALUES (" + index_subfamily + "," + index_family + ",'" + R.string.f1s1_nivelantes + "','" + R.string.f0_descripcion + "','" + R.drawable.nivelante80 + "');");
        sqlDB.execSQL("INSERT INTO Product VALUES (" + index_product + "," + index_subfamily + ",'" + R.string.f1s1p1_ni10 + "','" + R.string.f0_descripcion + "','" + R.drawable.nivelante80 + "','" + url + "');");
        sqlDB.execSQL("INSERT INTO Product VALUES (" + (index_product + 1) + "," + index_subfamily + ",'" + R.string.f1s1p2_ni80 + "','" + R.string.f0_descripcion + "','" + R.drawable.nivelante80 + "','" + url + "');");
        sqlDB.execSQL("INSERT INTO Product VALUES (" + (index_product + 2) + "," + index_subfamily + ",'" + R.string.f1s1p3_beL15 + "','" + R.string.f0_descripcion + "','" + R.drawable.nivelante80 + "','" + url + "');");
        sqlDB.execSQL("INSERT INTO Product VALUES (" + (index_product + 3) + "," + index_subfamily + ",'" + R.string.f1s1p4_beL30 + "','" + R.string.f0_descripcion + "','" + R.drawable.nivelante80 + "','" + url + "');");
        sqlDB.execSQL("INSERT INTO Product VALUES (" + (index_product + 4) + "," + index_subfamily + ",'" + R.string.f1s1p5_beP + "','" + R.string.f0_descripcion + "','" + R.drawable.nivelante80 + "','" + url + "');");

问题在于任何索引的价值都会一直增加,并且将来会增加更多的产品和系列。

我想知道是否有一些方法更有效,例如我使用这种方法:(index_product + 1) ; (index_product + 2); ...而不使用+N,因为将来会添加产品(或系列,子系列)并删除。而且很多产品(超过100种)可能会非常危险。

希望我能解释一下我的问题。

1 个答案:

答案 0 :(得分:1)

根据老板的说法,对于产品,子系列以及有什么东西以及使用外键来处理所有事情,可能更有意义。因此,每个家长都会有一个指向他们所有孩子的外键。

如果使用ORM,很可能会在幕后自动完成。

同样正如@Weston所提到的,避免在SQL语句中进行字符串连接。因为您的应用程序很容易进行SQL注入。