MYSQL:如何将数组添加到表中?
一个2048个单元的数组,如何将所有元素添加到表的对应字段中?
比如:
int array[2048],
table table1,表字段为ID,p0,p1,p2,...p2047;
array[0]插入到p0,array[1]插入到p1,array[2]插入到p2,
如何在表table1中添加2048个数据字段:pi?
如下创建表:
create table table1
(
ID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (ID)
)ENGINE = InnoDB;
再添加数据字段:
alter table table1 add p0 SMALLINT NULL;
如何使用函数循环添加剩余的字段?
使用如下语句,出错:
alter table table1 add CONCAT('P',CHAR(ASCII(i))) SMALLINT NULL;
一个2048个单元的数组,如何将所有元素添加到表的对应字段中?
比如:
int array[2048],
table table1,表字段为ID,p0,p1,p2,...p2047;
array[0]插入到p0,array[1]插入到p1,array[2]插入到p2,
如何在表table1中添加2048个数据字段:pi?
如下创建表:
create table table1
(
ID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (ID)
)ENGINE = InnoDB;
再添加数据字段:
alter table table1 add p0 SMALLINT NULL;
如何使用函数循环添加剩余的字段?
使用如下语句,出错:
alter table table1 add CONCAT('P',CHAR(ASCII(i))) SMALLINT NULL;
即使有很多字段,想将数组中的数据插入到表中你也只能在应用程序中进行,或者写一个php脚本执行。mysql 没有数组的概念
如何将数组插入到表中? Java + MySQL
CCD传感器采集数据,添加到数据库中,便于后续处理。
由于CCD像素数量2048,加上主键共2049列,其中数据列命名为p0、p1、p2...p2047,如果用命令行输入,简直累死个人了。且MySQL字段名仅支持串常量(MySQL5.5),pi字段名不易实现。设想:Java创建表,先创建仅一个字段(主键)的表,然后再逐列添加数据字段,如下:
public static void createRowDataTableAlter(String tableNameString)
{
Statement statement = null; try
{
// 2. 创建查询语句
statement = (Statement) connection.createStatement();
// 3. 创建表头
String tableString = "create table " + tableNameString + "(rdNumb INT NOT NULL AUTO_INCREMENT PRIMARY KEY);";
statement.execute( tableString );
// 4. 添加数据列
for ( int i = 0; i < 2048; i++ )
{
String statementString = "alter table " + tableNameString + " add p" + i + " SMALLINT ZEROFILL";
statement.execute(statementString);
}
}
catch ( SQLException e )
{
e.printStackTrace();
}
finally
{
try
{
if ( statement != null )
{
statement.close();
}
}
catch ( SQLException e )
{
e.printStackTrace();
}
}
}
创建一个2049列的表居然耗时:153206毫秒!
由于涉及修改表的操作,可能是耗时大的主要原因。
也可能与自动提交有关。设想:将创建表的语句集成到一个语句中,一次提交整个语句,如下:
public static void createRowDataTable(String tableNameString)
{
Statement statement = null;
try
{
// 2. 创建语句对象
statement = (Statement) connection.createStatement();
// 3. 集成创建表语句
String tableString = "create table " + tableNameString + "(rdNumb INT NOT NULL AUTO_INCREMENT PRIMARY KEY";
// 4. 添加数据字段
for ( int i = 0; i < 2048; i++ )
{
tableString += ", p" + i + " SMALLINT ZEROFILL";
}
// 5. 添加数据库引擎
tableString += " ) ENGINE = MYISAM;";
// 6. 执行创建表语句
statement.execute(tableString);
}
catch ( SQLException e )
{
e.printStackTrace();
}
finally
{
try
{
if ( statement != null )
{
statement.close();
}
}
catch ( SQLException e )
{
e.printStackTrace();
}
}
}
创建表是耗时锐减到207毫秒。两种方法耗时相差153206/207-1=740-1=739倍。main方法如下:
public static void main ( String[] args )
{
// 1. 建立数据库连接
connection = DataBaseManager.getConnection();
if(connection == null)
{
System.out.println( "连接失败!" );
return;
}
long startTime = 0;
long stopTime = 0;
System.out.println( "创建数据表开始..." );
startTime = System.currentTimeMillis();
// createRowDataTableAlter(tableNameString);
createRowDataTable(tableNameString);
stopTime = System.currentTimeMillis();
System.out.println( "创建数据表完成!消耗时间" );
System.out.println( stopTime - startTime ); fillArray(sarr);
System.out.println( "添加一条数据表记录开始..." );
startTime = System.currentTimeMillis();
addTableValue(sarr, tableNameString);
stopTime = System.currentTimeMillis();
System.out.println( "添加一条数据表记录完成!消耗时间" );
System.out.println( stopTime - startTime );
DataBaseManager.close( connection );
}创建数据表开始...
创建数据表完成!消耗时间
207
添加一条数据表记录开始...
添加一条数据表记录完成!消耗时间
29