发布网友 发布时间:2022-04-22 09:15
共6个回答
懂视网 时间:2022-05-01 04:36
) 一个组织Contract类的好方式是放置全局数据库的定义在这个类的根层。然后为每一个表创建一个内部类,枚举他们的列。 sunshine的这个WeatherContract类,定义了全局的URI路径:// The "Content authority" is a name for the entire content provider, similar to the // relationship between a domain name and its website. A convenient string to use for the // content authority is the package name for the app, which is guaranteed to be unique on the // device. public static final String CONTENT_AUTHORITY = "com.loveqiqi.sy.mysunshine"; // Use CONTENT_AUTHORITY to create the base of all URI‘s which apps will use to contact // the content provider. public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY); // Possible paths (appended to base content URI for possible URI‘s) // For instance, content://com.example.android.sunshine.app/weather/ is a valid path for // looking at weather data. content://com.example.android.sunshine.app/givemeroot/ will fail, // as the ContentProvider hasn‘t been given any information on what to do with "givemeroot". // At least, let‘s hope not. Don‘t be that dev, reader. Don‘t be that dev. public static final String PATH_WEATHER = "weather"; public static final String PATH_LOCATION = "location";
/* Inner class that defines the table contents of the location table Students: This is where you will add the strings. (Similar to what has been done for WeatherEntry) */ public static final class LocationEntry implements BaseColumns { public static final String TABLE_NAME = "location"; public static final String COLUMN_LOCATION_SETTING = "location_setting"; public static final String COLUMN_COORD_LAT = "coord_lat"; public static final String COLUMN_COORD_LONG = "coord_long"; public static final String COLUMN_CITY_NAME="city_name"; public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendPath(PATH_LOCATION).build(); public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_LOCATION; public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_LOCATION; public static Uri buildLocationUri(long id) { return ContentUris.withAppendedId(CONTENT_URI, id); } }
/** * Manages a local database for weather data. */ public class WeatherDbHelper extends SQLiteOpenHelper { // If you change the database schema, you must increment the database version. private static final int DATABASE_VERSION = 2; static final String DATABASE_NAME = "weather.db"; public WeatherDbHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { final String SQL_CREATE_WEATHER_TABLE = "CREATE TABLE " + WeatherEntry.TABLE_NAME + " (" + // Why AutoIncrement here, and not above? // Unique keys will be auto-generated in either case. But for weather // forecasting, it‘s reasonable to assume the user will want information // for a certain date and all dates *following*, so the forecast data // should be sorted accordingly. WeatherEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + // the ID of the location entry associated with this weather data WeatherEntry.COLUMN_LOC_KEY + " INTEGER NOT NULL, " + WeatherEntry.COLUMN_DATE + " INTEGER NOT NULL, " + WeatherEntry.COLUMN_SHORT_DESC + " TEXT NOT NULL, " + WeatherEntry.COLUMN_WEATHER_ID + " INTEGER NOT NULL," + WeatherEntry.COLUMN_MIN_TEMP + " REAL NOT NULL, " + WeatherEntry.COLUMN_MAX_TEMP + " REAL NOT NULL, " + WeatherEntry.COLUMN_HUMIDITY + " REAL NOT NULL, " + WeatherEntry.COLUMN_PRESSURE + " REAL NOT NULL, " + WeatherEntry.COLUMN_WIND_SPEED + " REAL NOT NULL, " + WeatherEntry.COLUMN_DEGREES + " REAL NOT NULL, " + // Set up the location column as a foreign key to location table. " FOREIGN KEY (" + WeatherEntry.COLUMN_LOC_KEY + ") REFERENCES " + LocationEntry.TABLE_NAME + " (" + LocationEntry._ID + "), " + // To assure the application have just one weather entry per day // per location, it‘s created a UNIQUE constraint with REPLACE strategy " UNIQUE (" + WeatherEntry.COLUMN_DATE + ", " + WeatherEntry.COLUMN_LOC_KEY + ") ON CONFLICT REPLACE);"; final String SQL_CREATE_LOACTION_TABLE = "CREATE TABLE " + LocationEntry.TABLE_NAME + "(" + LocationEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + LocationEntry.COLUMN_LOCATION_SETTING + " TEXT UNIQUE NOT NULL," + LocationEntry.COLUMN_COORD_LAT + " REAL NOT NULL,"+ LocationEntry.COLUMN_COORD_LONG + " REAL NOT NULL," + LocationEntry.COLUMN_CITY_NAME + " TEXT NOT NULL); "; sqLiteDatabase.execSQL(SQL_CREATE_WEATHER_TABLE); sqLiteDatabase.execSQL(SQL_CREATE_LOACTION_TABLE); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { // This database is only a cache for online data, so its upgrade policy is // to simply to discard the data and start over // Note that this only fires if you change the version number for your database. // It does NOT depend on the version number for your application. // If you want to update the schema without wiping data, commenting out the next 2 lines // should be your top priority before modifying this method. sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + LocationEntry.TABLE_NAME); sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + WeatherEntry.TABLE_NAME); onCreate(sqLiteDatabase); } }
onCreate(SQLiteDatabase)
,onUpgrade(SQLiteDatabase,
int, int)
and/oronOpen(SQLiteDatabase)
will be
called.所以,
数据库的创建是在我们第一次调用这个函数的时候。数据库才被创建。
mOpenHelper = new WeatherDbHelper(getContext());然后就是对数据库的增删改查了。
// Gets the data repository in write mode SQLiteDatabase db = mDbHelper.getWritableDatabase(); // Create a new map of values, where column names are the keys ContentValues values = new ContentValues(); values.put(FeedEntry.COLUMN_NAME_ENTRY_ID, id); values.put(FeedEntry.COLUMN_NAME_TITLE, title); values.put(FeedEntry.COLUMN_NAME_CONTENT, content); // Insert the new row, returning the primary key value of the new row long newRowId; newRowId = db.insert( FeedEntry.TABLE_NAME, FeedEntry.COLUMN_NAME_NULLABLE, values);第一个参数是表名,第二个参数是设置哪些列可以为空,如果设置为null,那么系统不会插入一个空行),一般是null吧,这样在定义 数据库的时候就需要知道哪些列是可为空的?如果要设置第二个参数的话.
SQLiteDatabase db = mDbHelper.getReadableDatabase(); // Define a projection that specifies which columns from the database // you will actually use after this query. String[] projection = { FeedEntry._ID, FeedEntry.COLUMN_NAME_TITLE, FeedEntry.COLUMN_NAME_UPDATED, ... }; // How you want the results sorted in the resulting Cursor String sortOrder = FeedEntry.COLUMN_NAME_UPDATED + " DESC"; Cursor c = db.query( FeedEntry.TABLE_NAME, // The table to query projection, // The columns to return selection, // The columns for the WHERE clause selectionArgs, // The values for the WHERE clause null, // don‘t group the rows null, // don‘t filter by row groups sortOrder // The sort order );
This method will return false if the cursor is empty然后就可以使用了:
cursor.moveToFirst(); long itemId = cursor.getLong( cursor.getColumnIndexOrThrow(FeedEntry._ID) );
// Define ‘where‘ part of query. String selection = FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?"; // Specify arguments in placeholder order. String[] selectionArgs = { String.valueOf(rowId) }; // Issue SQL statement. db.delete(table_name, selection, selectionArgs);
public int delete(String table,String
whereClause,
String[] whereArgs)
Returns
SQLiteDatabase db = mDbHelper.getWritableDatabase(); // New value for one column ContentValues values = new ContentValues(); values.put(FeedEntry.COLUMN_NAME_TITLE, title); // Which row to update, based on the ID String selection = FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?"; String[] selectionArgs = { String.valueOf(rowId) }; int count = db.update( FeedReaderDbHelper.FeedEntry.TABLE_NAME, values, selection, selectionArgs);
数据存储(2)使用SQL数据库
标签:
热心网友 时间:2022-05-01 01:44
用iamge类型,写入的时间用流写入,读出的时间同样需要流转换。
sql数据库存放视频是可以的,但是不推荐这么做,一般要把视频文件以二进制流的方式写入数据库字段,这样会消耗性能,读取也不方便。
但是现实中一般是推荐存放视频文件路径,比如把这种文件(图片、音频、视频)保存到一个专门的路径下, 而数据库只保存这个文件的完整路径即可。
调用时从数据库中取路径, 然后把相关的文件显示出来即可。
扩展资料:
SQL Server数据库包括Microsoft SQL Server以及Sybase SQL Server两个子数据库,该数据库能否正常运行直接关系着整个计算机系统的运行安全。
SQL包括了所有对数据库的操作,主要是由4个部分组成:
1、数据定义:又称为“DDL语言”,定义数据库的逻辑结构,包括定义数据库、基本表、视图和索引4部分。
2、数据操纵:又称为“DML语言”,包括插入、删除和更新三种操作。
3、数据查询:又称为“DQL语言”,包括数据查询操作。
4、数据控制:又称为“DCL语言”,对用户访问数据的控制有基本表和视图的授权及回收。
5、事务控制:又称为“TCL语言”,包括事务的提交与回滚。
6、嵌入式SQL语言的使用规定:规定SQL语句在宿主语言的程序中使用的规则。
参考资料来源:百度百科-SQL数据库
热心网友 时间:2022-05-01 03:02
1、sql数据库存放视频是可以的,但是不推荐这么做,一般要把视频文件以二进制流的方式写入数据库字段,这样会消耗性能,读取也不方便。
2、但是现实中一般是推荐存放视频文件路径,比如把这种文件(图片、音频、视频)保存到一个专门的路径下, 而数据库只保存这个文件的完整路径即可。
调用时从数据库中取路径, 然后把相关的文件显示出来即可。
热心网友 时间:2022-05-01 04:37
把数据库的字段设为image类型
不过最好还是不要把视频放到数据库,有很多的不好.
最好把视频文件存放到某一文件夹目录下,而在数据库中存放该视频文件所在的文件路径.
这样程序只要到数据库读取文件的存放路径,再根据这个路径去调用视频文件
热心网友 时间:2022-05-01 06:28
用iamge类型,写入的时间用流写入,读出的时间同样需要流转换
热心网友 时间:2022-05-01 08:36
背景
MySQL 一直以来都有 TEXT、BLOB 等类型用来存储图片、视频等大对象信息。比如一张图片,随便一张都 5M 以上。视频也是,随便一部视频就是 2G 以上。
假设用 MySQL 来存放电影视频等信息,一部是 2G,那么存储 1000 部就是 2TB,2TB 也就是 1000 条记录而已,但是对数据库性能来说,不仅仅是看记录数量,更主要的还得看占用磁盘空间大小。空间大了,所有以前的经验啥的都失效了。
所以一般来说存放这类信息,也就是存储他们的存放路径,至于文件本身存放在哪里,那这就不是数据库考虑的范畴了。数据库只关心怎么来的快,怎么来的小。
举例
虽然不推荐 MySQL 这样做,但是也得知道 MySQL 该怎么做才行,做到心里有数。比如下面一张微信图片,大概 5M 的样子。
root@ytt:/var/lib/mysql-files# ls -sihl 微信图片_20190711095019.jpg274501 5.4M -rw-r--r-- 1 root root 5.4M Jul 11 07:17 微信图片_20190711095019.jpg
拷贝 100 份这样的图片来测试
root@ytt:/var/lib/mysql-files# for i in `seq 1 100`; do cp 微信图片_20190711095019.jpg "$i".jpg;done;
root@ytt:/var/lib/mysql-files# ls
100.jpg 17.jpg 25.jpg 33.jpg 41.jpg 4.jpg 58.jpg 66.jpg 74.jpg 82.jpg 90.jpg 99.jpg f8.tsv
10.jpg 18.jpg 26.jpg 34.jpg 42.jpg 50.jpg 59.jpg 67.jpg 75.jpg 83.jpg 91.jpg 9.jpg 微信图片_20190711095019.jpg
1111.jpg 19.jpg 27.jpg 35.jpg 43.jpg 51.jpg 5.jpg 68.jpg 76.jpg 84.jpg 92.jpg f1.tsv
11.jpg 1.jpg 28.jpg 36.jpg 44.jpg 52.jpg 60.jpg 69.jpg 77.jpg 85.jpg 93.jpg f2.tsv
12.jpg 20.jpg 29.jpg 37.jpg 45.jpg 53.jpg 61.jpg 6.jpg 78.jpg 86.jpg 94.jpg f3.tsv
13.jpg 21.jpg 2.jpg 38.jpg 46.jpg 54.jpg 62.jpg 70.jpg 79.jpg 87.jpg 95.jpg f4.tsv
14.jpg 22.jpg 30.jpg 39.jpg 47.jpg 55.jpg 63.jpg 71.jpg 7.jpg 88.jpg 96.jpg f5.tsv
15.jpg 23.jpg 31.jpg 3.jpg 48.jpg 56.jpg .jpg 72.jpg 80.jpg .jpg 97.jpg f6.tsv
16.jpg 24.jpg 32.jpg 40.jpg 49.jpg 57.jpg 65.jpg 73.jpg 81.jpg 8.jpg 98.jpg f7.tsv
我们建三张表,分别用 LONGBLOB、LONGTEXT 和 VARCHAR 来存储这些图片信息
mysql> show create table tt_image1G
*************************** 1. row ***************************
Table: tt_image1
Create Table: CREATE TABLE `tt_image1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image_file` longblob,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
mysql> show create table tt_image2G
*************************** 1. row ***************************
Table: tt_image2
Create Table: CREATE TABLE `tt_image2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image_file` longtext,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
mysql> show create table tt_image3G
*************************** 1. row ***************************
Table: tt_image3
Create Table: CREATE TABLE `tt_image3` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image_file` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
我们来给三张表插入 100 张图片(插入前,建议把 max_allowed_packet 设置到最大)
tt_image1
root@ytt:/var/lib/mysql-files# for i in `seq 1 100`;
do mysql -S /var/run/mysqld/mysqld.sock -e "insert into ytt.tt_image1(image_file)
values (load_file('/var/lib/mysql-files/$i.jpg'))";done;
tt_image2
root@ytt:/var/lib/mysql-files# for i in `seq 1 100`;
do mysql -S /var/run/mysqld/mysqld.sock -e "insert into ytt.tt_image2(image_file)
values (hex(load_file('/var/lib/mysql-files/$i.jpg')))";done;
tt_image3
root@ytt:/var/lib/mysql-files# aa='begin;';for i in `seq 1 100`;
do aa=$aa"insert into ytt.tt_image3(image_file) values
('/var/lib/mysql-files/$i.jpg');";
done;aa=$aa'commit;';mysql -S /var/run/mysqld/mysqld.sock -e "`echo $aa`";
检查下三张表记录数
mysql> select 'tt_image1' as name ,count(*) from tt_image1 union allselect 'tt_image2',count(*) from tt_image2 union all select 'tt_image3', count(*) from tt_image3;+-----------+----------+| name | count(*) |+-----------+----------+| tt_image1 | 100 || tt_image2 | 100 || tt_image3 | 100 |+-----------+----------+3 rows in set (0.00 sec)看下文件大小,可以看到实际大小排名,LONGTEXT 字段存储的最大,LONGBLOB 字段缩小到一半,最小的是存储图片路径的表 tt_image3。所以这里从存储空间来看,存放路径最占优势。
root@ytt:/var/lib/mysql/ytt# ls -silhS tt_image*274603 1.1G -rw-r----- 1 mysql mysql 1.1G Jul 11 07:27 tt_image2.ibd274602 545M -rw-r----- 1 mysql mysql 544M Jul 11 07:26 tt_image1.ibd274605 80K -rw-r----- 1 mysql mysql 112K Jul 11 07:27 tt_image3.ibd那么怎么把图片取出来呢?
tt_image3 肯定是最容易的
mysql> select * from tt_image3;+----+----------------------------+| id | image_file |+----+----------------------------+| 1 | /var/lib/mysql-files/1.jpg |+----+----------------------------+...100 rows in set (0.00 sec)tt_image1 直接导出来二进制文件即可,下面我写了个存储过程,导出所有图片。
mysql> DELIMITER $$mysql> USE `ytt`$$mysql> DROP PROCEDURE IF EXISTS `sp_get_image`$$mysql> CREATE DEFINER=`ytt`@`localhost` PROCEDURE `sp_get_image`()mysql> BEGIN DECLARE i,cnt INT DEFAULT 0; SELECT COUNT(*) FROM tt_image1 WHERE 1 INTO cnt; WHILE i < cnt DO SET @stmt = CONCAT('select image_file from tt_image1 limit ',i,',1 into mpfile ''/var/lib/mysql-files/image',i,'.jpg'''); PREPARE s1 FROM @stmt; EXECUTE s1; DROP PREPARE s1; SET i = i + 1; END WHILE; END$$mysql> DELIMITER ;mysql> call sp_get_image;tt_image2 类似,把 select 语句里 image_file 变为 unhex(image_file) 即可。
总结
这里我举了个用 MySQL 来存放图片的例子,总的来说有以下三点:
占用磁盘空间大(这样会带来各种各样的功能与性能问题,比如备份,写入,读取操作等)
使用不易
还是推荐用文件路径来代替实际的文件内容存放