ORACLE里头动态的添加字段,如果存在就不添加,如果不存在就添加。sql语句怎么写?()
admin
2023-06-29 11:33:31
0次
相机,尼康SQ,
declare p_table_name varchar2(30);p_column_name varchar2(30);p_data_type varchar2(30);p_cnt number;p_sql varchar2(4000);begin p_table_name:=''; p_column_name:=''; select count(1) into p_cnt from user_tab_cols where a.table_name=p_table_name and a.column_name=p_column_name; if p_cnt=0 then p_sql:='alter table '||p_table_name||' add '||p_column_name||' '||p_data_type; execute immediate p_sql; end if;end;没测试,不过基本应该可以
这里我们要用Cursor中的getCount()方法去获得表中数据的行数(即,是否有数据)
注意!此方法返回值是int型,为数据行数,有数据则大于等于1无数据则为0,
拿到查询回来的行数后在java代码中判断是否为0,即是否有数据!看下面代码:
//创建或者打开数据库
SQLiteDatabase db = openOrCreateDatabase(test.db, Context.MODE_PRIVATE, null);
//实例化一个Cursor 为c做查询
Cursor c=db.rawQuery(select * from person where id=1,null);
int re = c.getCount();//定义一个int型变量re接收查询行数的返回值
//对re(数据行数)进行判断,为0执行下面insert,非0执行update if(re==0){
db.execSQL(insert into person(id,name,password) values(1,2,3));
}else{
db.execSQL(update person set name=? ,password=? where id=1,new Object[]{name,password});
}
基本每一行都有注释,纯手打。SQL语句改一下即可套用
///sqlite数据表建立唯一限制就行了么///sql代码如下:CREATE TABLE main.film (filmno INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,filmname TEXT(100),language TEXT(10),type TEXT(10),CONSTRAINT 不重复规则 UNIQUE (filmnam...
相关内容