将声音存入SQLite3数据库

iPhone开发中,很多时候要用到播放声音,但是直接将声音放入工程中势必造成Resources中文件太多,这样就需要将声音存入SQLite3数据库。以下为代码:


public class WriteVoice2DB {

public void WriteVoice2DB() {
System.out.println("Create WriteVoice2DB instance!");
}

public static void main(String[] args) {
try {
WriteVoice2DB wvdb = new WriteVoice2DB();
wvdb.savabinay();

} catch (Exception e) {
e.printStackTrace();
}

}


public void savabinay() throws rdeSQLiteException {
rdeSqlite3DB database = new rdeSqlite3DB();
database.open("Spanish_voice1.dat");
database.ExecSQL("CREATE TABLE voice_binary_table('id' CHAR(30) PRIMARY KEY, 'filename' CHAR(30), 'audioBinary' blob)");

//read the file stream
byte[] dataArray = {};
try {
File f = new File("C:/Documents and Settings/liu/My Documents/My Music/waiting.wav");
dataArray = read2list(f);
System.out.println(dataArray.length);

//save in DB
StringBuffer sb = new StringBuffer();
String insertSql = "INSERT INTO voice_binary_table(id,audioBinary) VALUES(?,?);";
Object[] parims = new Object[2];

parims[0] = new String(f.getName());
parims[1] = dataArray;

database.ExecSQL(insertSql, sb, parims);
System.out.println("the tail is " + sb.toString());

database.ExecSQL(insertSql);
} catch (Exception e) {
e.printStackTrace();
}

database.close();
System.out.println("SaveBinary Successful!");

}


public byte[] read2list(File file) throws IOException {

InputStream is = null;
byte[] buf = null;
int bufLen = 20000*1024;

try {
is = new BufferedInputStream(new FileInputStream(file));
buf = new byte[bufLen];
byte[] tmp = null;
int len = 0;

ArrayList data = new ArrayList(24);
while ((len = is.read(buf, 0, bufLen)) != -1) {
tmp = new byte[len];
System.arraycopy(buf, 0, tmp, 0, len);
data.add(tmp);
}

len = 0;
if (data.size() == 1)
return (byte[])data.get(0);
for (int i = 0; i < data.size(); i++) {
tmp = (byte[])data.get(i);
System.arraycopy(tmp, 0, buf, len, tmp.length);

len += tmp.length;
}

} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}

return buf;
}

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Python内置的sqlite3模块来将数据存入SQLite数据库中。步骤如下: 1. 导入sqlite3模块 ```python import sqlite3 ``` 2. 连接到SQLite数据库 ```python conn = sqlite3.connect('example.db') ``` 其中,`example.db`是数据库文件名,如果该文件不存在,则会自动创建。 3. 创建表格 ```python conn.execute('''CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER NOT NULL);''') ``` 该语句创建了一个名为`users`的表格,包含三个字段:`id`、`name`和`age`。 4. 插入数据 ```python conn.execute("INSERT INTO users (name, age) VALUES (?, ?)", ('John', 25)) ``` 该语句插入了一条数据,将`name`设置为`John`,`age`设置为`25`。 5. 查询数据 ```python cursor = conn.execute("SELECT * FROM users") for row in cursor: print("ID = ", row[0]) print("Name = ", row[1]) print("Age = ", row[2]) ``` 该语句查询了`users`表格中的所有数据,并依次打印出每条数据的`id`、`name`和`age`字段。 6. 关闭连接 ```python conn.close() ``` 完整代码如下: ```python import sqlite3 # 连接到SQLite数据库 conn = sqlite3.connect('example.db') # 创建表格 conn.execute('''CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER NOT NULL);''') # 插入数据 conn.execute("INSERT INTO users (name, age) VALUES (?, ?)", ('John', 25)) # 查询数据 cursor = conn.execute("SELECT * FROM users") for row in cursor: print("ID = ", row[0]) print("Name = ", row[1]) print("Age = ", row[2]) # 关闭连接 conn.close() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值