SQLite数据库拼写工具类

前端之家收集整理的这篇文章主要介绍了SQLite数据库拼写工具类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

为了避免打关键字出错,特意写的工具类




/**
* sql字符串追加器
* @author Sam
*
*/
public class sqlBuilder
{
private StringBuilder builder;
private final String TAB = " ";

public sqlBuilder()
{
builder = new StringBuilder();
}

public sqlBuilder addsql(String value)
{
builder.append(TAB+value+TAB);
return this;
}

/**
* 添加左括号
* @return
*/
public sqlBuilder addLeftBracket()
{
builder.append("(");
return this;
}

/**
* 添加integer类型
* @return
*/
public sqlBuilder addInt()
{
this.addsql("integer");
return this;
}

/**
* 添加text类型
* @return
*/
public sqlBuilder addText()
{
this.addsql("text");
return this;
}

/**
* 追加varchar
* @param len 长度
* @return
*/
public sqlBuilder addVarchar(int len)
{
this.addsql("varchar").addLeftBracket().add(len).addRightBracket();
return this;
}

/**
* 添加右括号
* @return
*/
public sqlBuilder addRightBracket()
{
builder.append(")");
return this;
}


/**
* 添加逗号
* @return
*/
public sqlBuilder addComma()
{
builder.append(",");
return this;
}

/**
* 追加not null
* @return
*/
public sqlBuilder addNotNull()
{
this.addsql("not null");
return this;
}

public sqlBuilder addNull()
{
this.addsql("null");
return this;
}

public sqlBuilder addPrimarykey()
{
this.addsql("primary key");
return this;
}

public sqlBuilder add(Object value)
{
builder.append(value);
return this;
}


/**
* 添加自增长字段
* @return
*/
public sqlBuilder addAutoincrement()
{
this.addsql("autoincrement");
return this;
}

/**
* "create table if not exists"
* @return
*/
public sqlBuilder addCreateTableIfNotExists()
{
this.addsql("create table if not exists");
return this;
}

/**
* 添加属性字段
* @param element
* @return
*/
public sqlBuilder addElement(String element)
{
this.addsql(element);
return this;
}

public String toString()
{
return builder.toString();
}

}

--------------------------------------------------------------------------------------------------------------------------------------

public static void main(String[] args)
{
sqlBuilder builder = new sqlBuilder();
builder.addCreateTableIfNotExists().addElement("student");
builder.addLeftBracket();
builder.addElement("id").addInt().addNotNull().addPrimarykey().addAutoincrement().addComma();
builder.addElement("age").addInt().addNull().addComma();
builder.addElement("name").addVarchar(25).addNull().addComma();
builder.addElement("info").addText().addNull();
builder.addRightBracket();
System.out.println(builder);
}

---------------------------------------------------------------------------------------------

猜你在找的Sqlite相关文章