使用 sql 从数据库中获取图像 scr
fetching image scr from database using sql
我设法从 table table 的内容栏中获取了一张图片的详细信息。我想我需要把它放在存储过程中。但我不太确定如何放置它或从哪里开始。我需要从 table.
的内容栏中获取所有图像细节,而不仅仅是一张图像
uri
content
id
/websites/
<src="https://static.google.png">...< src="https://static.yahoo.png">
1
/website1/
<src="https://static.google2.png">...< src="https://static.yahoo3.png">
2
仅我当前查询returns
SELECT SUBSTR(src, 1, LOCATE('png',src)+2) AS htt from
(select substr(
substr(content, locate('src="', content) + 5), 1 ) as 'src'
from exampletable
WHERE substr(
substr(content, locate('src="', content) + 5), 1 ) LIKE "https%"
)A
WHERE SUBSTR(src, 1, LOCATE('png',src)+2) LIKE '%png'
AND SUBSTR(src, 1, LOCATE('png',src)+2) NOT LIKE '%\<%'
;
最好在您的应用程序中执行此操作
如果复制存储过程,需要在开头和结尾添加DELIMITER
,只有Mysql Workbench自动添加。 DBfiddle也是这样,所以例子中没有
CREATE TABLE table1
(`uri` varchar(10), `content` varchar(71), `id` int)
;
INSERT INTO table1
(`uri`, `content`, `id`)
VALUES
('/websites/', '<src="https://static.google.png">...< src="https://static.yahoo.png">', 1),
('/website1/', '<src="https://static.google2.png">...< src="https://static.yahoo3.png">', 2)
;
CREATE PROCEDURE `getimages`()
BEGIN
DECLARE finished INTEGER DEFAULT 0;
DECLARE _content TEXT DEFAULT "";
-- declare cursor for employee email
DEClARE curcontent
CURSOR FOR
SELECT `content` FROM table1;
-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
DROP TEMPORARY TABLE IF EXISTS new_tbl;
CREATE TEMPORARY TABLE new_tbl (link VARCHAR(100));
OPEN curcontent;
getsrc: LOOP
FETCH curcontent INTO _content;
IF finished = 1 THEN
LEAVE getsrc;
END IF;
-- build email list
getimg: LOOP
sET @a := locate('src="', _content);
if locate('src="', _content) = 0 THEN
LEAVE getimg;
END IF;
INSERT INTO new_tbl VALUES (LEFT(SUBSTR(_content, locate('src="', _content) + 5),locate('.png"', substr(_content, locate('src="', _content) + 5)) +3));
IF _content != RIGHT(_content, LOCATE('png',_content)+9) then
SET _content := RIGHT(_content, LOCATE('png',_content)+9);
ELSE
LEAVE getimg;
END IF;
IF _content IS NULL then
LEAVE getimg;
END IF;
IF LEnGTH (_content) < 4 then
LEAVE getimg;
END IF;
END LOOP getimg;
END LOOP getsrc;
CLOSE curcontent;
SELECT DISTINCT * FROM new_tbl;
END
cALL getimages()
| link |
| :------------------------- |
| https://static.google.png |
| https://static.yahoo.png |
| https://static.google2.png |
| https://static.yahoo3.png |
✓
db<>fiddle here
我设法从 table table 的内容栏中获取了一张图片的详细信息。我想我需要把它放在存储过程中。但我不太确定如何放置它或从哪里开始。我需要从 table.
的内容栏中获取所有图像细节,而不仅仅是一张图像uri | content | id |
---|---|---|
/websites/ | <src="https://static.google.png">...< src="https://static.yahoo.png"> | 1 |
/website1/ | <src="https://static.google2.png">...< src="https://static.yahoo3.png"> | 2 |
仅我当前查询returns
SELECT SUBSTR(src, 1, LOCATE('png',src)+2) AS htt from
(select substr(
substr(content, locate('src="', content) + 5), 1 ) as 'src'
from exampletable
WHERE substr(
substr(content, locate('src="', content) + 5), 1 ) LIKE "https%"
)A
WHERE SUBSTR(src, 1, LOCATE('png',src)+2) LIKE '%png'
AND SUBSTR(src, 1, LOCATE('png',src)+2) NOT LIKE '%\<%'
;
最好在您的应用程序中执行此操作
如果复制存储过程,需要在开头和结尾添加DELIMITER
,只有Mysql Workbench自动添加。 DBfiddle也是这样,所以例子中没有
CREATE TABLE table1 (`uri` varchar(10), `content` varchar(71), `id` int) ; INSERT INTO table1 (`uri`, `content`, `id`) VALUES ('/websites/', '<src="https://static.google.png">...< src="https://static.yahoo.png">', 1), ('/website1/', '<src="https://static.google2.png">...< src="https://static.yahoo3.png">', 2) ;
CREATE PROCEDURE `getimages`() BEGIN DECLARE finished INTEGER DEFAULT 0; DECLARE _content TEXT DEFAULT ""; -- declare cursor for employee email DEClARE curcontent CURSOR FOR SELECT `content` FROM table1; -- declare NOT FOUND handler DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1; DROP TEMPORARY TABLE IF EXISTS new_tbl; CREATE TEMPORARY TABLE new_tbl (link VARCHAR(100)); OPEN curcontent; getsrc: LOOP FETCH curcontent INTO _content; IF finished = 1 THEN LEAVE getsrc; END IF; -- build email list getimg: LOOP sET @a := locate('src="', _content); if locate('src="', _content) = 0 THEN LEAVE getimg; END IF; INSERT INTO new_tbl VALUES (LEFT(SUBSTR(_content, locate('src="', _content) + 5),locate('.png"', substr(_content, locate('src="', _content) + 5)) +3)); IF _content != RIGHT(_content, LOCATE('png',_content)+9) then SET _content := RIGHT(_content, LOCATE('png',_content)+9); ELSE LEAVE getimg; END IF; IF _content IS NULL then LEAVE getimg; END IF; IF LEnGTH (_content) < 4 then LEAVE getimg; END IF; END LOOP getimg; END LOOP getsrc; CLOSE curcontent; SELECT DISTINCT * FROM new_tbl; END
cALL getimages()
| link | | :------------------------- | | https://static.google.png | | https://static.yahoo.png | | https://static.google2.png | | https://static.yahoo3.png | ✓
db<>fiddle here